46 #include "Epetra_MpiComm.h" 49 #include "Epetra_SerialComm.h" 52 #include "Teuchos_ParameterList.hpp" 53 #include "Teuchos_RCP.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_ConfigDefs.h" 61 #include "Epetra_Map.h" 62 #include "Epetra_CrsGraph.h" 63 #include "Epetra_FECrsGraph.h" 64 #include "Epetra_RowMatrix.h" 65 #include "Epetra_CrsMatrix.h" 66 #include "Epetra_FECrsMatrix.h" 67 #include "Epetra_MultiVector.h" 68 #include "Epetra_Import.h" 75 static void Tokenize(
const std::string& str, std::vector<std::string>& tokens,
76 const std::string& delimiters =
" ")
79 std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
81 std::string::size_type pos = str.find_first_of(delimiters, lastPos);
83 while (std::string::npos != pos || std::string::npos != lastPos)
86 tokens.push_back(str.substr(lastPos, pos - lastPos));
88 lastPos = str.find_first_not_of(delimiters, pos);
90 pos = str.find_first_of(delimiters, lastPos);
99 #ifdef HAVE_TEUCHOS_EXPAT 100 FileInputSource fileSrc(FileName);
101 fileXML_ = rcp(
new XMLObject(fileSrc.getObject()));
104 std::cerr <<
"Teuchos was not configured with support for expat." << std::endl;
105 std::cerr <<
"Please reconfigure teuchos with --enable-teuchos-expat." << std::endl;
111 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES 113 Read(
const std::string& Label, Epetra_CrsGraph*& Graph)
115 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
116 "No file has been opened");
120 for (
int i = 0; i < fileXML_->numChildren(); ++i)
122 const XMLObject& child = fileXML_->getChild(i);
123 std::string tag = child.getTag();
127 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
130 int NumGlobalRows = child.getRequiredInt(
"Rows");
131 int NumGlobalCols = child.getRequiredInt(
"Columns");
132 int NumGlobalEntries = child.getRequiredInt(
"Entries");
133 int Offset = child.getRequiredInt(
"StartingIndex");
134 if (debug) std::cout << NumGlobalCols << NumGlobalEntries << Offset << std::endl;
136 Epetra_Map map(NumGlobalRows, 0, Comm_);
137 Graph =
new Epetra_CrsGraph(Copy, map, 0);
139 for (
int j = 0; j < child.numContentLines(); ++j)
141 std::vector<std::string> tokens;
142 const std::string& line = child.getContentLine(j);
144 if (tokens.size() < 2)
continue;
147 row = atoi((
char*)tokens[0].c_str());
148 col = atoi((
char*)tokens[1].c_str());
150 if (map.LID(row) != -1)
151 Graph->InsertGlobalIndices(row, 1, &col);
153 Graph->FillComplete();
160 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES 162 Read64(
const std::string& Label, Epetra_CrsGraph*& Graph)
164 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
165 "No file has been opened");
169 for (
int i = 0; i < fileXML_->numChildren(); ++i)
171 const XMLObject& child = fileXML_->getChild(i);
172 std::string tag = child.getTag();
176 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
179 long long NumGlobalRows = child.getRequired<
long long>(
"Rows");
180 long long NumGlobalCols = child.getRequired<
long long>(
"Columns");
181 long long NumGlobalEntries = child.getRequired<
long long>(
"Entries");
182 int Offset = child.getRequiredInt(
"StartingIndex");
183 if (debug) std::cout << NumGlobalCols << NumGlobalEntries << Offset << std::endl;
185 Epetra_Map map(NumGlobalRows, 0, Comm_);
186 Graph =
new Epetra_CrsGraph(Copy, map, 0);
188 for (
int j = 0; j < child.numContentLines(); ++j)
190 std::vector<std::string> tokens;
191 const std::string& line = child.getContentLine(j);
193 if (tokens.size() < 2)
continue;
198 #if defined(_MSC_VER) 199 row = _strtoi64((
char*)tokens[0].c_str(), &endp, base);
200 col = _strtoi64((
char*)tokens[1].c_str(), &endp, base);
203 std::istringstream ss_row(tokens[0]);
205 std::istringstream ss_col(tokens[1]);
208 row = strtoll((
char*)tokens[0].c_str(), &endp, base);
209 col = strtoll((
char*)tokens[1].c_str(), &endp, base);
213 if (map.LID(row) != -1)
214 Graph->InsertGlobalIndices(row, 1, &col);
216 Graph->FillComplete();
224 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES 226 Read(
const std::string& Label, Epetra_CrsMatrix*& matrix)
228 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
229 "No file has been opened");
233 for (
int i = 0; i < fileXML_->numChildren(); ++i)
235 const XMLObject& child = fileXML_->getChild(i);
236 std::string tag = child.getTag();
238 if (tag ==
"PointMatrix")
240 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
243 int NumGlobalRows = child.getRequiredInt(
"Rows");
244 int NumGlobalCols = child.getRequiredInt(
"Columns");
245 int NumGlobalNonzeros = child.getRequiredInt(
"Nonzeros");
246 int Offset = child.getRequiredInt(
"StartingIndex");
247 if (debug) std::cout << NumGlobalCols << NumGlobalNonzeros << Offset << std::endl;
249 Epetra_Map map(NumGlobalRows, 0, Comm_);
250 matrix =
new Epetra_CrsMatrix(Copy, map, 0);
252 for (
int j = 0; j < child.numContentLines(); ++j)
254 std::vector<std::string> tokens;
255 const std::string& line = child.getContentLine(j);
257 if (tokens.size() < 3)
continue;
261 row = atoi((
char*)tokens[0].c_str());
262 col = atoi((
char*)tokens[1].c_str());
263 sscanf((
char*)tokens[2].c_str(),
"%lg", &val);
266 if (map.LID(row) != -1)
267 matrix->InsertGlobalValues(row, 1, &val, &col);
269 matrix->FillComplete();
276 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES 278 Read64(
const std::string& Label, Epetra_CrsMatrix*& matrix)
280 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
281 "No file has been opened");
285 for (
int i = 0; i < fileXML_->numChildren(); ++i)
287 const XMLObject& child = fileXML_->getChild(i);
288 std::string tag = child.getTag();
290 if (tag ==
"PointMatrix")
292 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
295 long long NumGlobalRows = child.getRequiredInt(
"Rows");
296 long long NumGlobalCols = child.getRequiredInt(
"Columns");
297 long long NumGlobalNonzeros = child.getRequiredInt(
"Nonzeros");
298 int Offset = child.getRequiredInt(
"StartingIndex");
299 if (debug) std::cout << NumGlobalCols << NumGlobalNonzeros << Offset << std::endl;
301 Epetra_Map map(NumGlobalRows, 0, Comm_);
302 matrix =
new Epetra_CrsMatrix(Copy, map, 0);
304 for (
int j = 0; j < child.numContentLines(); ++j)
306 std::vector<std::string> tokens;
307 const std::string& line = child.getContentLine(j);
309 if (tokens.size() < 3)
continue;
315 #if defined(_MSC_VER) 316 row = _strtoi64((
char*)tokens[0].c_str(), &endp, base);
317 col = _strtoi64((
char*)tokens[1].c_str(), &endp, base);
320 std::istringstream ss_row(tokens[0]);
322 std::istringstream ss_col(tokens[1]);
325 row = strtoll((
char*)tokens[0].c_str(), &endp, base);
326 col = strtoll((
char*)tokens[1].c_str(), &endp, base);
329 sscanf((
char*)tokens[2].c_str(),
"%lg", &val);
332 if (map.LID(row) != -1)
333 matrix->InsertGlobalValues(row, 1, &val, &col);
335 matrix->FillComplete();
342 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES 344 Read(
const std::string& Label, Epetra_MultiVector*& MultiVector)
346 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
347 "No file has been opened");
352 for (
int i = 0; i < fileXML_->numChildren(); ++i)
354 const XMLObject& child = fileXML_->getChild(i);
355 std::string tag = child.getTag();
357 if (tag ==
"MultiVector")
359 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
361 int GlobalLength = child.getRequiredInt(
"Length");
362 int NumVectors = child.getRequiredInt(
"NumVectors");
364 Epetra_Map Map(GlobalLength, 0, Comm_);
365 MultiVector =
new Epetra_MultiVector(Map, NumVectors);
369 for (
int j = 0; j < child.numContentLines(); ++j)
371 std::vector<std::string> tokens;
373 const std::string& line = child.getContentLine(j);
377 if (tokens.size() == 0)
continue;
379 TEUCHOS_TEST_FOR_EXCEPTION(tokens.size() != (unsigned) NumVectors, std::logic_error,
380 "wrong number of tokens in line; " 381 <<
"tokens.size() = " << tokens.size()
382 <<
", NumVectors = " << NumVectors);
383 int tsize = (int) tokens.size();
384 for (
int k = 0; k < tsize; ++k)
386 if (Map.LID(count) != -1)
388 sscanf((
char*)(tokens[k].c_str()),
"%lf", &val);
390 (*MultiVector)[k][Map.LID(count)] = val;
401 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES 403 Read64(
const std::string& Label, Epetra_MultiVector*& MultiVector)
405 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
406 "No file has been opened");
411 for (
int i = 0; i < fileXML_->numChildren(); ++i)
413 const XMLObject& child = fileXML_->getChild(i);
414 std::string tag = child.getTag();
416 if (tag ==
"MultiVector")
418 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
420 long long GlobalLength = child.getRequired<
long long>(
"Length");
421 int NumVectors = child.getRequiredInt(
"NumVectors");
423 Epetra_Map Map(GlobalLength, 0, Comm_);
424 MultiVector =
new Epetra_MultiVector(Map, NumVectors);
428 for (
long long j = 0; j < child.numContentLines(); ++j)
430 std::vector<std::string> tokens;
432 const std::string& line = child.getContentLine(j);
436 if (tokens.size() == 0)
continue;
438 TEUCHOS_TEST_FOR_EXCEPTION(tokens.size() != (unsigned) NumVectors, std::logic_error,
439 "wrong number of tokens in line; " 440 <<
"tokens.size() = " << tokens.size()
441 <<
", NumVectors = " << NumVectors);
442 int tsize = (int) tokens.size();
443 for (
int k = 0; k < tsize; ++k)
445 if (Map.LID(count) != -1)
447 sscanf((
char*)(tokens[k].c_str()),
"%lf", &val);
449 (*MultiVector)[k][Map.LID(count)] = val;
460 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES 462 Read(
const std::string& Label, Epetra_Map*& Map)
464 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
465 "No file has been opened");
470 for (
int i = 0; i < fileXML_->numChildren(); ++i)
472 const XMLObject& child = fileXML_->getChild(i);
473 std::string tag = child.getTag();
477 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
479 int NumGlobalElements = child.getRequiredInt(
"NumElements");
480 int IndexBase = child.getRequiredInt(
"IndexBase");
481 int NumProc = child.getRequiredInt(
"NumProc");
483 TEUCHOS_TEST_FOR_EXCEPTION(NumProc != Comm_.NumProc(), std::logic_error,
484 "Requested map defined with different number of processors, " 485 <<
"NumProc = " << NumProc <<
" while " 486 <<
"Comm.NumProc() = " << Comm_.NumProc());
489 sprintf(str,
"ElementsOnProc%d", Comm_.MyPID());
490 int NumMyElements = child.getRequiredInt(str);
492 sprintf(str,
"ElementsOnProc%d", Comm_.MyPID());
494 std::vector<int> MyGlobalElements(NumMyElements);
496 for (
int iproc = 0; iproc < child.numChildren(); ++iproc)
498 const XMLObject& newChild = child.getChild(iproc);
501 if (newChild.hasAttribute(
"ID") &&
502 newChild.getRequiredInt(
"ID") == Comm_.MyPID())
504 for (
int j = 0; j < newChild.numContentLines(); ++j)
506 std::vector<std::string> tokens;
508 const std::string& line = newChild.getContentLine(j);
511 int tsize = (int) tokens.size();
512 for (
int k = 0; k < tsize; ++k)
514 MyGlobalElements[count++] = atoi((
char*)tokens[k].c_str());
520 Map =
new Epetra_Map(NumGlobalElements, NumMyElements,
521 &MyGlobalElements[0], IndexBase, Comm_);
528 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES 530 Read64(
const std::string& Label, Epetra_Map*& Map)
532 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
533 "No file has been opened");
538 for (
int i = 0; i < fileXML_->numChildren(); ++i)
540 const XMLObject& child = fileXML_->getChild(i);
541 std::string tag = child.getTag();
545 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
547 long long NumGlobalElements = child.getRequired<
long long>(
"NumElements");
548 long long IndexBase = child.getRequired<
long long>(
"IndexBase");
549 int NumProc = child.getRequiredInt(
"NumProc");
551 TEUCHOS_TEST_FOR_EXCEPTION(NumProc != Comm_.NumProc(), std::logic_error,
552 "Requested map defined with different number of processors, " 553 <<
"NumProc = " << NumProc <<
" while " 554 <<
"Comm.NumProc() = " << Comm_.NumProc());
557 sprintf(str,
"ElementsOnProc%d", Comm_.MyPID());
558 int NumMyElements = child.getRequiredInt(str);
560 sprintf(str,
"ElementsOnProc%d", Comm_.MyPID());
562 std::vector<long long> MyGlobalElements(NumMyElements);
564 for (
int iproc = 0; iproc < child.numChildren(); ++iproc)
566 const XMLObject& newChild = child.getChild(iproc);
569 if (newChild.hasAttribute(
"ID") &&
570 newChild.getRequiredInt(
"ID") == Comm_.MyPID())
572 for (
int j = 0; j < newChild.numContentLines(); ++j)
574 std::vector<std::string> tokens;
576 const std::string& line = newChild.getContentLine(j);
579 int tsize = (int) tokens.size();
580 for (
int k = 0; k < tsize; ++k)
584 #if defined(_MSC_VER) 585 MyGlobalElements[count++] = _strtoi64((
char*)tokens[k].c_str(), &endp, base);
588 std::istringstream ss(tokens[k]);
589 ss >> MyGlobalElements[count++];
591 MyGlobalElements[count++] = strtoll((
char*)tokens[k].c_str(), &endp, base);
599 Map =
new Epetra_Map(NumGlobalElements, NumMyElements,
600 &MyGlobalElements[0], IndexBase, Comm_);
609 Read(
const std::string& Label, std::vector<std::string>& Content)
611 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
612 "No file has been opened");
614 for (
int i = 0; i < fileXML_->numChildren(); ++i)
616 const XMLObject& child = fileXML_->getChild(i);
617 std::string tag = child.getTag();
621 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
623 for (
int j = 0; j < child.numContentLines(); ++j)
625 const std::string& line = child.getContentLine(j);
626 if (line ==
"\n")
continue;
627 Content.push_back(line);
636 Read(
const std::string& Label, Teuchos::ParameterList& List)
638 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
639 "No file has been opened");
641 for (
int i = 0; i < fileXML_->numChildren(); ++i)
643 const XMLObject& child = fileXML_->getChild(i);
644 std::string tag = child.getTag();
648 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
650 Teuchos::XMLParameterListReader ListReader;
651 List = ListReader.toParameterList(child.getChild(0));
void Read(const std::string &Label, Epetra_Map *&Map)
Reads the Epetra_Map stored with label Label.
Teuchos::RCP< Teuchos::XMLObject > fileXML_
parsed XML object.
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
void Read64(const std::string &Label, Epetra_Map *&Map)
Reads the Epetra_Map stored with label Label. Long Long version.
bool IsOpen_
If true, then the file has been successfully opened.