EpetraExt  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EpetraExt_XMLWriter.cpp
Go to the documentation of this file.
1 /*
2 //@HEADER
3 // ***********************************************************************
4 //
5 // EpetraExt: Epetra Extended - Linear Algebra Services Package
6 // Copyright (2011) Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 //@HEADER
42 */
43 
44 #include "EpetraExt_ConfigDefs.h"
45 #ifdef HAVE_MPI
46 #include "Epetra_MpiComm.h"
47 #include "mpi.h"
48 #else
49 #include "Epetra_SerialComm.h"
50 #endif
51 #include "EpetraExt_XMLWriter.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"
59 
60 using namespace Teuchos;
61 
62 // ============================================================================
64 XMLWriter(const Epetra_Comm& comm, const std::string& FileName) :
65  Comm_(comm),
66  FileName_(FileName),
67  IsOpen_(false)
68 {}
69 
70 // ============================================================================
72 Create(const std::string& Label)
73 {
74  if (Comm_.MyPID() == 0)
75  {
76  std::ofstream of(FileName_.c_str());
77  of << "<ObjectCollection Label=\"" << Label << "\">" << std::endl;
78  of.close();
79  }
80 
81  IsOpen_ = true;
82 }
83 
84 // ============================================================================
86 {
87  if (Comm_.MyPID() == 0)
88  {
89  std::ofstream of(FileName_.c_str(), std::ios::app);
90  of << "</ObjectCollection>" << std::endl;
91  of.close();
92  }
93 
94  IsOpen_ = false;
95 }
96 
97 // ============================================================================
99 Write(const std::string& Label, const std::vector<std::string>& Content)
100 {
101  TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
102  "No file has been opened");
103 
104  if (Comm_.MyPID()) return;
105 
106  std::ofstream of(FileName_.c_str(), std::ios::app);
107 
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;
112 
113  of << "</Text>" << std::endl;
114 
115  of.close();
116 }
117 
118 // ============================================================================
120 Write(const std::string& Label, const Epetra_RowMatrix& Matrix)
121 {
122  TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
123  "No file has been opened");
124 
125  long long Rows = Matrix.NumGlobalRows64();
126  long long Cols = Matrix.NumGlobalRows64();
127  long long Nonzeros = Matrix.NumGlobalNonzeros64();
128 
129  if (Comm_.MyPID() == 0)
130  {
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;
137  }
138 
139  int Length = Matrix.MaxNumEntries();
140  std::vector<int> Indices(Length);
141  std::vector<double> Values(Length);
142 
143  for (int iproc = 0; iproc < Comm_.NumProc(); iproc++)
144  {
145  if (iproc == Comm_.MyPID())
146  {
147  std::ofstream of(FileName_.c_str(), std::ios::app);
148  of.precision(15);
149 
150  for (int i = 0; i < Matrix.NumMyRows(); ++i)
151  {
152  int NumMyEntries;
153  Matrix.ExtractMyRowCopy(i, Length, NumMyEntries, &Values[0], &Indices[0]);
154 
155  long long GRID = Matrix.RowMatrixRowMap().GID64(i);
156 
157  for (int j = 0; j < NumMyEntries; ++j)
158  of << GRID << " " << Matrix.RowMatrixColMap().GID64(Indices[j])
159  << " " << std::setiosflags(std::ios::scientific) << Values[j] << std::endl;
160  }
161  of.close();
162  }
163  Comm_.Barrier();
164  }
165 
166  if (Comm_.MyPID() == 0)
167  {
168  std::ofstream of(FileName_.c_str(), std::ios::app);
169  of << "</PointMatrix>" << std::endl;
170  of.close();
171  }
172 }
173 
174 // ============================================================================
176 Write(const std::string& Label, const Epetra_MultiVector& MultiVector)
177 {
178  TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
179  "No file has been opened");
180 
181  long long Length = MultiVector.GlobalLength64();
182  int NumVectors = MultiVector.NumVectors();
183 
184  if (Comm_.MyPID() == 0)
185  {
186  std::ofstream of(FileName_.c_str(), std::ios::app);
187 
188  of << "<MultiVector Label=\"" << Label
189  << "\" Length=\"" << Length << '"'
190  << " NumVectors=\"" << NumVectors << '"'
191  << " Type=\"double\">" << std::endl;
192  }
193 
194 
195  for (int iproc = 0; iproc < Comm_.NumProc(); iproc++)
196  {
197  if (iproc == Comm_.MyPID())
198  {
199  std::ofstream of(FileName_.c_str(), std::ios::app);
200 
201  of.precision(15);
202  for (int i = 0; i < MultiVector.MyLength(); ++i)
203  {
204  for (int j = 0; j < NumVectors; ++j)
205  of << std::setiosflags(std::ios::scientific) << MultiVector[j][i] << " ";
206  of << std::endl;
207  }
208  of.close();
209  }
210  Comm_.Barrier();
211  }
212 
213  if (Comm_.MyPID() == 0)
214  {
215  std::ofstream of(FileName_.c_str(), std::ios::app);
216  of << "</MultiVector>" << std::endl;
217  of.close();
218  }
219 }
220 
221 // ============================================================================
223 Write(const std::string& Label, const Epetra_Map& Map)
224 {
225  TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
226  "No file has been opened");
227 
228  long long NumGlobalElements = Map.NumGlobalElements64();
229  const int* MyGlobalElements_int = 0;
230  const long long* MyGlobalElements_LL = 0;
231  Map.MyGlobalElements(MyGlobalElements_int, MyGlobalElements_LL);
232 
233  if(!MyGlobalElements_int || !MyGlobalElements_LL)
234  throw "EpetraExt::XMLWriter::Write: ERROR, GlobalIndices type unknown.";
235 
236  if (Comm_.MyPID() == 0)
237  {
238  std::ofstream of(FileName_.c_str(), std::ios::app);
239 
240  of << "<Map Label=\"" << Label
241  << "\" NumElements=\"" << NumGlobalElements << '"'
242  << " IndexBase=\"" << Map.IndexBase64() << '"'
243  << " NumProc=\"" << Comm_.NumProc() << '"';
244 
245  of.close();
246  }
247 
248  for (int iproc = 0; iproc < Comm_.NumProc(); ++iproc)
249  {
250  if (iproc == Comm_.MyPID())
251  {
252  std::ofstream of(FileName_.c_str(), std::ios::app);
253 
254  of << " ElementsOnProc" << iproc << "=\"" << Map.NumMyElements() << '"';
255  of.close();
256  }
257  Comm_.Barrier();
258  }
259 
260  if (Comm_.MyPID() == 0)
261  {
262  std::ofstream of(FileName_.c_str(), std::ios::app);
263  of << '>' << std::endl;
264  of.close();
265  }
266 
267  for (int iproc = 0; iproc < Comm_.NumProc(); iproc++)
268  {
269  if (iproc == Comm_.MyPID())
270  {
271  std::ofstream of(FileName_.c_str(), std::ios::app);
272 
273  of << "<Proc ID=\"" << Comm_.MyPID() << "\">" << std::endl;
274 
275  if(MyGlobalElements_int)
276  {
277  for (int i = 0; i < Map.NumMyElements(); ++i)
278  {
279  of << MyGlobalElements_int[i] << std::endl;
280  }
281  }
282  else
283  {
284  for (int i = 0; i < Map.NumMyElements(); ++i)
285  {
286  of << MyGlobalElements_LL[i] << std::endl;
287  }
288  }
289 
290  of << "</Proc>" << std::endl;
291  of.close();
292  }
293  Comm_.Barrier();
294  }
295 
296  if (Comm_.MyPID() == 0)
297  {
298  std::ofstream of(FileName_.c_str(), std::ios::app);
299  of << "</Map>" << std::endl;
300  of.close();
301  }
302 }
303 
304 // ============================================================================
305 void EpetraExt::XMLWriter::
306 Write(const std::string& Label, Teuchos::ParameterList& List)
307 {
308  TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
309  "No file has been opened");
310 
311  if (Comm_.MyPID()) return;
312 
313  std::ofstream of(FileName_.c_str(), std::ios::app);
314 
315  of << "<List Label=\"" << Label << "\">" << std::endl;
316 
317  XMLParameterListWriter Writer;
318  XMLObject Obj = Writer.toXML(List);
319 
320  of << Obj.toString();
321 
322  of << "</List>" << std::endl;
323 
324  of.close();
325 }
virtual const Epetra_Map & RowMatrixRowMap() const =0
int MyGlobalElements(int *MyGlobalElementList) const
XMLWriter(const Epetra_Comm &Comm, const std::string &FileName)
ctor
int NumMyElements() const
virtual int MaxNumEntries() const =0
void Create(const std::string &Label)
Creates the file, giving Label to the whole object.
virtual int NumMyRows() const =0
virtual int ExtractMyRowCopy(int MyRow, int Length, int &NumEntries, double *Values, int *Indices) const =0
virtual const Epetra_Map & RowMatrixColMap() const =0
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.