46 #include "Epetra_MpiComm.h"
49 #include "Epetra_SerialComm.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"
70 static void Tokenize(
const std::string& str, std::vector<std::string>& tokens,
71 const std::string& delimiters =
" ")
74 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
76 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
78 while (std::string::npos != pos || std::string::npos != lastPos)
81 tokens.push_back(str.substr(lastPos, pos - lastPos));
83 lastPos = str.find_first_not_of(delimiters, pos);
85 pos = str.find_first_of(delimiters, lastPos);
88 using namespace Teuchos;
94 #ifdef HAVE_TEUCHOS_EXPAT
95 FileInputSource fileSrc(FileName);
96 fileXML_ = rcp(
new XMLObject(fileSrc.getObject()));
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;
107 Read(
const std::string& Label, Epetra_CrsGraph*& Graph)
109 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
110 "No file has been opened");
114 for (
int i = 0; i < fileXML_->numChildren(); ++i)
116 const XMLObject& child = fileXML_->getChild(i);
117 std::string tag = child.getTag();
121 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
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;
130 Epetra_Map map(NumGlobalRows, 0, Comm_);
131 Graph =
new Epetra_CrsGraph(Copy, map, 0);
133 for (
int j = 0; j < child.numContentLines(); ++j)
135 std::vector<std::string> tokens;
136 const std::string& line = child.getContentLine(j);
138 if (tokens.size() < 2)
continue;
141 row = atoi((
char*)tokens[0].c_str());
142 col = atoi((
char*)tokens[1].c_str());
144 if (map.LID(row) != -1)
145 Graph->InsertGlobalIndices(row, 1, &col);
147 Graph->FillComplete();
155 Read(
const std::string& Label, Epetra_CrsMatrix*& matrix)
157 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
158 "No file has been opened");
162 for (
int i = 0; i < fileXML_->numChildren(); ++i)
164 const XMLObject& child = fileXML_->getChild(i);
165 std::string tag = child.getTag();
167 if (tag ==
"PointMatrix")
169 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
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;
178 Epetra_Map map(NumGlobalRows, 0, Comm_);
179 matrix =
new Epetra_CrsMatrix(Copy, map, 0);
181 for (
int j = 0; j < child.numContentLines(); ++j)
183 std::vector<std::string> tokens;
184 const std::string& line = child.getContentLine(j);
186 if (tokens.size() < 3)
continue;
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);
195 if (map.LID(row) != -1)
196 matrix->InsertGlobalValues(row, 1, &val, &col);
198 matrix->FillComplete();
206 Read(
const std::string& Label, Epetra_MultiVector*& MultiVector)
208 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
209 "No file has been opened");
214 for (
int i = 0; i < fileXML_->numChildren(); ++i)
216 const XMLObject& child = fileXML_->getChild(i);
217 std::string tag = child.getTag();
219 if (tag ==
"MultiVector")
221 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
223 int GlobalLength = child.getRequiredInt(
"Length");
224 int NumVectors = child.getRequiredInt(
"NumVectors");
226 Epetra_Map Map(GlobalLength, 0, Comm_);
227 MultiVector =
new Epetra_MultiVector(Map, NumVectors);
231 for (
int j = 0; j < child.numContentLines(); ++j)
233 std::vector<std::string> tokens;
235 const std::string& line = child.getContentLine(j);
239 if (tokens.size() == 0)
continue;
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)
248 if (Map.LID(count) != -1)
250 sscanf((
char*)(tokens[k].c_str()),
"%lf", &val);
252 (*MultiVector)[k][Map.LID(count)] = val;
264 Read(
const std::string& Label, Epetra_Map*& Map)
266 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
267 "No file has been opened");
272 for (
int i = 0; i < fileXML_->numChildren(); ++i)
274 const XMLObject& child = fileXML_->getChild(i);
275 std::string tag = child.getTag();
279 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
281 int NumGlobalElements = child.getRequiredInt(
"NumElements");
282 int IndexBase = child.getRequiredInt(
"IndexBase");
283 int NumProc = child.getRequiredInt(
"NumProc");
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());
291 sprintf(str,
"ElementsOnProc%d", Comm_.MyPID());
292 int NumMyElements = child.getRequiredInt(str);
294 sprintf(str,
"ElementsOnProc%d", Comm_.MyPID());
296 std::vector<int> MyGlobalElements(NumMyElements);
298 for (
int iproc = 0; iproc < child.numChildren(); ++iproc)
300 const XMLObject& newChild = child.getChild(iproc);
303 if (newChild.hasAttribute(
"ID") &&
304 newChild.getRequiredInt(
"ID") == Comm_.MyPID())
306 for (
int j = 0; j < newChild.numContentLines(); ++j)
308 std::vector<std::string> tokens;
310 const std::string& line = newChild.getContentLine(j);
313 int tsize = (int) tokens.size();
314 for (
int k = 0; k < tsize; ++k)
316 MyGlobalElements[count++] = atoi((
char*)tokens[k].c_str());
322 Map =
new Epetra_Map(NumGlobalElements, NumMyElements,
323 &MyGlobalElements[0], IndexBase, Comm_);
331 Read(
const std::string& Label, std::vector<std::string>& Content)
333 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
334 "No file has been opened");
336 for (
int i = 0; i < fileXML_->numChildren(); ++i)
338 const XMLObject& child = fileXML_->getChild(i);
339 std::string tag = child.getTag();
343 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
345 for (
int j = 0; j < child.numContentLines(); ++j)
347 const std::string& line = child.getContentLine(j);
348 if (line ==
"\n")
continue;
349 Content.push_back(line);
358 Read(
const std::string& Label, Teuchos::ParameterList& List)
360 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
361 "No file has been opened");
363 for (
int i = 0; i < fileXML_->numChildren(); ++i)
365 const XMLObject& child = fileXML_->getChild(i);
366 std::string tag = child.getTag();
370 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
372 Teuchos::XMLParameterListReader ListReader;
373 List = ListReader.toParameterList(child.getChild(0));
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