EpetraExt  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EpetraExt_XMLReader.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_XMLReader.h"
52 #include "Teuchos_ParameterList.hpp"
53 #include "Teuchos_RefCountPtr.hpp"
54 #include "Teuchos_XMLObject.hpp"
55 #include "Teuchos_StringInputSource.hpp"
56 #include "Teuchos_FileInputSource.hpp"
57 #include "Teuchos_ParameterList.hpp"
58 #include "Teuchos_XMLParameterListReader.hpp"
59 #include "Teuchos_Assert.hpp"
60 #include "Epetra_Map.h"
61 #include "Epetra_CrsGraph.h"
62 #include "Epetra_FECrsGraph.h"
63 #include "Epetra_RowMatrix.h"
64 #include "Epetra_CrsMatrix.h"
65 #include "Epetra_FECrsMatrix.h"
66 #include "Epetra_MultiVector.h"
67 #include "Epetra_Import.h"
68 
69 // ============================================================================
70 static void Tokenize(const std::string& str, std::vector<std::string>& tokens,
71  const std::string& delimiters = " ")
72 {
73  // Skip delimiters at beginning.
74  std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
75  // Find first "non-delimiter".
76  std::string::size_type pos = str.find_first_of(delimiters, lastPos);
77 
78  while (std::string::npos != pos || std::string::npos != lastPos)
79  {
80  // Found a token, add it to the std::vector.
81  tokens.push_back(str.substr(lastPos, pos - lastPos));
82  // Skip delimiters. Note the "not_of"
83  lastPos = str.find_first_not_of(delimiters, pos);
84  // Find next "non-delimiter"
85  pos = str.find_first_of(delimiters, lastPos);
86  }
87 }
88 using namespace Teuchos;
89 
90 // ============================================================================
91 EpetraExt::XMLReader::XMLReader(const Epetra_Comm& comm, const std::string& FileName) :
92  Comm_(comm)
93 {
94 #ifdef HAVE_TEUCHOS_EXPAT
95  FileInputSource fileSrc(FileName);
96  fileXML_ = rcp(new XMLObject(fileSrc.getObject()));
97  IsOpen_ = true;
98 #else
99  std::cerr << "Teuchos was not configured with support for expat." << std::endl;
100  std::cerr << "Please reconfigure teuchos with --enable-teuchos-expat." << std::endl;
101  exit(EXIT_FAILURE);
102 #endif
103 }
104 
105 // ============================================================================
107 Read(const std::string& Label, Epetra_CrsGraph*& Graph)
108 {
109  TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
110  "No file has been opened");
111 
112  Graph = 0;
113 
114  for (int i = 0; i < fileXML_->numChildren(); ++i)
115  {
116  const XMLObject& child = fileXML_->getChild(i);
117  std::string tag = child.getTag();
118 
119  if (tag == "Graph")
120  {
121  if (child.hasAttribute("Label") && child.getRequired("Label") == Label)
122  {
123  bool debug = false;
124  int NumGlobalRows = child.getRequiredInt("Rows");
125  int NumGlobalCols = child.getRequiredInt("Columns");
126  int NumGlobalEntries = child.getRequiredInt("Entries");
127  int Offset = child.getRequiredInt("StartingIndex");
128  if (debug) std::cout << NumGlobalCols << NumGlobalEntries << Offset << std::endl;
129 
130  Epetra_Map map(NumGlobalRows, 0, Comm_);
131  Graph = new Epetra_CrsGraph(Copy, map, 0);
132 
133  for (int j = 0; j < child.numContentLines(); ++j)
134  {
135  std::vector<std::string> tokens;
136  const std::string& line = child.getContentLine(j);
137  Tokenize(line, tokens, " \n\r\t");
138  if (tokens.size() < 2) continue;
139 
140  int row, col;
141  row = atoi((char*)tokens[0].c_str());
142  col = atoi((char*)tokens[1].c_str());
143 
144  if (map.LID(row) != -1)
145  Graph->InsertGlobalIndices(row, 1, &col);
146  }
147  Graph->FillComplete();
148  }
149  }
150  }
151 }
152 
153 // ============================================================================
155 Read(const std::string& Label, Epetra_CrsMatrix*& matrix)
156 {
157  TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
158  "No file has been opened");
159 
160  matrix = 0;
161 
162  for (int i = 0; i < fileXML_->numChildren(); ++i)
163  {
164  const XMLObject& child = fileXML_->getChild(i);
165  std::string tag = child.getTag();
166 
167  if (tag == "PointMatrix")
168  {
169  if (child.hasAttribute("Label") && child.getRequired("Label") == Label)
170  {
171  bool debug = false;
172  int NumGlobalRows = child.getRequiredInt("Rows");
173  int NumGlobalCols = child.getRequiredInt("Columns");
174  int NumGlobalNonzeros = child.getRequiredInt("Nonzeros");
175  int Offset = child.getRequiredInt("StartingIndex");
176  if (debug) std::cout << NumGlobalCols << NumGlobalNonzeros << Offset << std::endl;
177 
178  Epetra_Map map(NumGlobalRows, 0, Comm_);
179  matrix = new Epetra_CrsMatrix(Copy, map, 0);
180 
181  for (int j = 0; j < child.numContentLines(); ++j)
182  {
183  std::vector<std::string> tokens;
184  const std::string& line = child.getContentLine(j);
185  Tokenize(line, tokens, " \n\r\t");
186  if (tokens.size() < 3) continue;
187 
188  int row, col;
189  double val;
190  row = atoi((char*)tokens[0].c_str());
191  col = atoi((char*)tokens[1].c_str());
192  sscanf((char*)tokens[2].c_str(), "%lg", &val);
193  //val = atof((char*)tokens[2].c_str());
194 
195  if (map.LID(row) != -1)
196  matrix->InsertGlobalValues(row, 1, &val, &col);
197  }
198  matrix->FillComplete();
199  }
200  }
201  }
202 }
203 
204 // ============================================================================
206 Read(const std::string& Label, Epetra_MultiVector*& MultiVector)
207 {
208  TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
209  "No file has been opened");
210 
211  MultiVector = 0;
212 
213  // read all file and create all objects in memory.
214  for (int i = 0; i < fileXML_->numChildren(); ++i)
215  {
216  const XMLObject& child = fileXML_->getChild(i);
217  std::string tag = child.getTag();
218 
219  if (tag == "MultiVector")
220  {
221  if (child.hasAttribute("Label") && child.getRequired("Label") == Label)
222  {
223  int GlobalLength = child.getRequiredInt("Length");
224  int NumVectors = child.getRequiredInt("NumVectors");
225 
226  Epetra_Map Map(GlobalLength, 0, Comm_);
227  MultiVector = new Epetra_MultiVector(Map, NumVectors);
228 
229  int count = 0;
230  double val;
231  for (int j = 0; j < child.numContentLines(); ++j)
232  {
233  std::vector<std::string> tokens;
234 
235  const std::string& line = child.getContentLine(j);
236 
237  Tokenize(line, tokens, " \n\r\t");
238 
239  if (tokens.size() == 0) continue;
240 
241  TEUCHOS_TEST_FOR_EXCEPTION(tokens.size() != (unsigned) NumVectors, std::logic_error,
242  "wrong number of tokens in line; "
243  << "tokens.size() = " << tokens.size()
244  << ", NumVectors = " << NumVectors);
245  int tsize = (int) tokens.size();
246  for (int k = 0; k < tsize; ++k)
247  {
248  if (Map.LID(count) != -1)
249  {
250  sscanf((char*)(tokens[k].c_str()), "%lf", &val);
251 
252  (*MultiVector)[k][Map.LID(count)] = val;
253  }
254  }
255  ++count;
256  }
257  }
258  }
259  }
260 }
261 
262 // ============================================================================
264 Read(const std::string& Label, Epetra_Map*& Map)
265 {
266  TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
267  "No file has been opened");
268 
269  Map = 0;
270 
271  // read all file and create all objects in memory.
272  for (int i = 0; i < fileXML_->numChildren(); ++i)
273  {
274  const XMLObject& child = fileXML_->getChild(i);
275  std::string tag = child.getTag();
276 
277  if (tag == "Map")
278  {
279  if (child.hasAttribute("Label") && child.getRequired("Label") == Label)
280  {
281  int NumGlobalElements = child.getRequiredInt("NumElements");
282  int IndexBase = child.getRequiredInt("IndexBase");
283  int NumProc = child.getRequiredInt("NumProc");
284 
285  TEUCHOS_TEST_FOR_EXCEPTION(NumProc != Comm_.NumProc(), std::logic_error,
286  "Requested map defined with different number of processors, "
287  << "NumProc = " << NumProc << " while "
288  << "Comm.NumProc() = " << Comm_.NumProc());
289 
290  char str[80];
291  sprintf(str, "ElementsOnProc%d", Comm_.MyPID());
292  int NumMyElements = child.getRequiredInt(str);
293 
294  sprintf(str, "ElementsOnProc%d", Comm_.MyPID());
295 
296  std::vector<int> MyGlobalElements(NumMyElements);
297 
298  for (int iproc = 0; iproc < child.numChildren(); ++iproc)
299  {
300  const XMLObject& newChild = child.getChild(iproc);
301  int count = 0;
302 
303  if (newChild.hasAttribute("ID") &&
304  newChild.getRequiredInt("ID") == Comm_.MyPID())
305  {
306  for (int j = 0; j < newChild.numContentLines(); ++j)
307  {
308  std::vector<std::string> tokens;
309 
310  const std::string& line = newChild.getContentLine(j);
311 
312  Tokenize(line, tokens, " \n\r\t");
313  int tsize = (int) tokens.size();
314  for (int k = 0; k < tsize; ++k)
315  {
316  MyGlobalElements[count++] = atoi((char*)tokens[k].c_str());
317  }
318  }
319  }
320  }
321 
322  Map = new Epetra_Map(NumGlobalElements, NumMyElements,
323  &MyGlobalElements[0], IndexBase, Comm_);
324  }
325  }
326  }
327 }
328 
329 // ============================================================================
331 Read(const std::string& Label, std::vector<std::string>& Content)
332 {
333  TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
334  "No file has been opened");
335 
336  for (int i = 0; i < fileXML_->numChildren(); ++i)
337  {
338  const XMLObject& child = fileXML_->getChild(i);
339  std::string tag = child.getTag();
340 
341  if (tag == "Text")
342  {
343  if (child.hasAttribute("Label") && child.getRequired("Label") == Label)
344  {
345  for (int j = 0; j < child.numContentLines(); ++j)
346  {
347  const std::string& line = child.getContentLine(j);
348  if (line == "\n") continue;
349  Content.push_back(line);
350  }
351  }
352  }
353  }
354 }
355 
356 // ============================================================================
358 Read(const std::string& Label, Teuchos::ParameterList& List)
359 {
360  TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ == false, std::logic_error,
361  "No file has been opened");
362 
363  for (int i = 0; i < fileXML_->numChildren(); ++i)
364  {
365  const XMLObject& child = fileXML_->getChild(i);
366  std::string tag = child.getTag();
367 
368  if (tag == "List")
369  {
370  if (child.hasAttribute("Label") && child.getRequired("Label") == Label)
371  {
372  Teuchos::XMLParameterListReader ListReader;
373  List = ListReader.toParameterList(child.getChild(0));
374  }
375  }
376  }
377 }
void Read(const std::string &Label, Epetra_Map *&Map)
Reads the Epetra_Map stored with label Label.
static void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
XMLReader(const Epetra_Comm &Comm, const std::string &FileName)
ctor