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);
93 using namespace Teuchos;
99 #ifdef HAVE_TEUCHOS_EXPAT
100 FileInputSource fileSrc(FileName);
101 fileXML_ = rcp(
new XMLObject(fileSrc.getObject()));
105 std::cerr <<
"Teuchos was not configured with support for expat." << std::endl;
106 std::cerr <<
"Please reconfigure teuchos with --enable-teuchos-expat." << std::endl;
112 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
116 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
117 "No file has been opened");
121 for (
int i = 0; i < fileXML_->numChildren(); ++i)
123 const XMLObject& child = fileXML_->getChild(i);
124 std::string tag = child.getTag();
128 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
131 int NumGlobalRows = child.getRequiredInt(
"Rows");
132 int NumGlobalCols = child.getRequiredInt(
"Columns");
133 int NumGlobalEntries = child.getRequiredInt(
"Entries");
134 int Offset = child.getRequiredInt(
"StartingIndex");
135 if (debug) std::cout << NumGlobalCols << NumGlobalEntries << Offset << std::endl;
140 for (
int j = 0; j < child.numContentLines(); ++j)
142 std::vector<std::string> tokens;
143 const std::string& line = child.getContentLine(j);
145 if (tokens.size() < 2)
continue;
148 row = atoi((
char*)tokens[0].c_str());
149 col = atoi((
char*)tokens[1].c_str());
151 if (map.
LID(row) != -1)
161 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
165 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
166 "No file has been opened");
170 for (
int i = 0; i < fileXML_->numChildren(); ++i)
172 const XMLObject& child = fileXML_->getChild(i);
173 std::string tag = child.getTag();
177 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
180 long long NumGlobalRows = child.getRequired<
long long>(
"Rows");
181 long long NumGlobalCols = child.getRequired<
long long>(
"Columns");
182 long long NumGlobalEntries = child.getRequired<
long long>(
"Entries");
183 int Offset = child.getRequiredInt(
"StartingIndex");
184 if (debug) std::cout << NumGlobalCols << NumGlobalEntries << Offset << std::endl;
189 for (
int j = 0; j < child.numContentLines(); ++j)
191 std::vector<std::string> tokens;
192 const std::string& line = child.getContentLine(j);
194 if (tokens.size() < 2)
continue;
199 #if defined(_MSC_VER)
200 row = _strtoi64((
char*)tokens[0].c_str(), &endp, base);
201 col = _strtoi64((
char*)tokens[1].c_str(), &endp, base);
204 std::istringstream ss_row(tokens[0]);
206 std::istringstream ss_col(tokens[1]);
209 row = strtoll((
char*)tokens[0].c_str(), &endp, base);
210 col = strtoll((
char*)tokens[1].c_str(), &endp, base);
214 if (map.
LID(row) != -1)
225 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
229 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
230 "No file has been opened");
234 for (
int i = 0; i < fileXML_->numChildren(); ++i)
236 const XMLObject& child = fileXML_->getChild(i);
237 std::string tag = child.getTag();
239 if (tag ==
"PointMatrix")
241 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
244 int NumGlobalRows = child.getRequiredInt(
"Rows");
245 int NumGlobalCols = child.getRequiredInt(
"Columns");
246 int NumGlobalNonzeros = child.getRequiredInt(
"Nonzeros");
247 int Offset = child.getRequiredInt(
"StartingIndex");
248 if (debug) std::cout << NumGlobalCols << NumGlobalNonzeros << Offset << std::endl;
253 for (
int j = 0; j < child.numContentLines(); ++j)
255 std::vector<std::string> tokens;
256 const std::string& line = child.getContentLine(j);
258 if (tokens.size() < 3)
continue;
262 row = atoi((
char*)tokens[0].c_str());
263 col = atoi((
char*)tokens[1].c_str());
264 sscanf((
char*)tokens[2].c_str(),
"%lg", &val);
267 if (map.
LID(row) != -1)
277 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
281 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
282 "No file has been opened");
286 for (
int i = 0; i < fileXML_->numChildren(); ++i)
288 const XMLObject& child = fileXML_->getChild(i);
289 std::string tag = child.getTag();
291 if (tag ==
"PointMatrix")
293 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
296 long long NumGlobalRows = child.getRequiredInt(
"Rows");
297 long long NumGlobalCols = child.getRequiredInt(
"Columns");
298 long long NumGlobalNonzeros = child.getRequiredInt(
"Nonzeros");
299 int Offset = child.getRequiredInt(
"StartingIndex");
300 if (debug) std::cout << NumGlobalCols << NumGlobalNonzeros << Offset << std::endl;
305 for (
int j = 0; j < child.numContentLines(); ++j)
307 std::vector<std::string> tokens;
308 const std::string& line = child.getContentLine(j);
310 if (tokens.size() < 3)
continue;
316 #if defined(_MSC_VER)
317 row = _strtoi64((
char*)tokens[0].c_str(), &endp, base);
318 col = _strtoi64((
char*)tokens[1].c_str(), &endp, base);
321 std::istringstream ss_row(tokens[0]);
323 std::istringstream ss_col(tokens[1]);
326 row = strtoll((
char*)tokens[0].c_str(), &endp, base);
327 col = strtoll((
char*)tokens[1].c_str(), &endp, base);
330 sscanf((
char*)tokens[2].c_str(),
"%lg", &val);
333 if (map.
LID(row) != -1)
343 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
347 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
348 "No file has been opened");
353 for (
int i = 0; i < fileXML_->numChildren(); ++i)
355 const XMLObject& child = fileXML_->getChild(i);
356 std::string tag = child.getTag();
358 if (tag ==
"MultiVector")
360 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
362 int GlobalLength = child.getRequiredInt(
"Length");
363 int NumVectors = child.getRequiredInt(
"NumVectors");
370 for (
int j = 0; j < child.numContentLines(); ++j)
372 std::vector<std::string> tokens;
374 const std::string& line = child.getContentLine(j);
378 if (tokens.size() == 0)
continue;
380 TEUCHOS_TEST_FOR_EXCEPTION(tokens.size() != (unsigned) NumVectors, std::logic_error,
381 "wrong number of tokens in line; "
382 <<
"tokens.size() = " << tokens.size()
383 <<
", NumVectors = " << NumVectors);
384 int tsize = (int) tokens.size();
385 for (
int k = 0; k < tsize; ++k)
387 if (Map.
LID(count) != -1)
389 sscanf((
char*)(tokens[k].c_str()),
"%lf", &val);
391 (*MultiVector)[k][Map.
LID(count)] = val;
402 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
406 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
407 "No file has been opened");
412 for (
int i = 0; i < fileXML_->numChildren(); ++i)
414 const XMLObject& child = fileXML_->getChild(i);
415 std::string tag = child.getTag();
417 if (tag ==
"MultiVector")
419 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
421 long long GlobalLength = child.getRequired<
long long>(
"Length");
422 int NumVectors = child.getRequiredInt(
"NumVectors");
429 for (
long long j = 0; j < child.numContentLines(); ++j)
431 std::vector<std::string> tokens;
433 const std::string& line = child.getContentLine(j);
437 if (tokens.size() == 0)
continue;
439 TEUCHOS_TEST_FOR_EXCEPTION(tokens.size() != (unsigned) NumVectors, std::logic_error,
440 "wrong number of tokens in line; "
441 <<
"tokens.size() = " << tokens.size()
442 <<
", NumVectors = " << NumVectors);
443 int tsize = (int) tokens.size();
444 for (
int k = 0; k < tsize; ++k)
446 if (Map.
LID(count) != -1)
448 sscanf((
char*)(tokens[k].c_str()),
"%lf", &val);
450 (*MultiVector)[k][Map.
LID(count)] = val;
461 #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES
465 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
466 "No file has been opened");
471 for (
int i = 0; i < fileXML_->numChildren(); ++i)
473 const XMLObject& child = fileXML_->getChild(i);
474 std::string tag = child.getTag();
478 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
480 int NumGlobalElements = child.getRequiredInt(
"NumElements");
481 int IndexBase = child.getRequiredInt(
"IndexBase");
482 int NumProc = child.getRequiredInt(
"NumProc");
484 TEUCHOS_TEST_FOR_EXCEPTION(NumProc != Comm_.NumProc(), std::logic_error,
485 "Requested map defined with different number of processors, "
486 <<
"NumProc = " << NumProc <<
" while "
487 <<
"Comm.NumProc() = " << Comm_.NumProc());
490 sprintf(str,
"ElementsOnProc%d", Comm_.MyPID());
491 int NumMyElements = child.getRequiredInt(str);
493 sprintf(str,
"ElementsOnProc%d", Comm_.MyPID());
495 std::vector<int> MyGlobalElements(NumMyElements);
497 for (
int iproc = 0; iproc < child.numChildren(); ++iproc)
499 const XMLObject& newChild = child.getChild(iproc);
502 if (newChild.hasAttribute(
"ID") &&
503 newChild.getRequiredInt(
"ID") == Comm_.MyPID())
505 for (
int j = 0; j < newChild.numContentLines(); ++j)
507 std::vector<std::string> tokens;
509 const std::string& line = newChild.getContentLine(j);
512 int tsize = (int) tokens.size();
513 for (
int k = 0; k < tsize; ++k)
515 MyGlobalElements[count++] = atoi((
char*)tokens[k].c_str());
521 Map =
new Epetra_Map(NumGlobalElements, NumMyElements,
522 &MyGlobalElements[0], IndexBase, Comm_);
529 #ifndef EPETRA_NO_64BIT_GLOBAL_INDICES
533 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
534 "No file has been opened");
539 for (
int i = 0; i < fileXML_->numChildren(); ++i)
541 const XMLObject& child = fileXML_->getChild(i);
542 std::string tag = child.getTag();
546 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
548 long long NumGlobalElements = child.getRequired<
long long>(
"NumElements");
549 long long IndexBase = child.getRequired<
long long>(
"IndexBase");
550 int NumProc = child.getRequiredInt(
"NumProc");
552 TEUCHOS_TEST_FOR_EXCEPTION(NumProc != Comm_.NumProc(), std::logic_error,
553 "Requested map defined with different number of processors, "
554 <<
"NumProc = " << NumProc <<
" while "
555 <<
"Comm.NumProc() = " << Comm_.NumProc());
558 sprintf(str,
"ElementsOnProc%d", Comm_.MyPID());
559 int NumMyElements = child.getRequiredInt(str);
561 sprintf(str,
"ElementsOnProc%d", Comm_.MyPID());
563 std::vector<long long> MyGlobalElements(NumMyElements);
565 for (
int iproc = 0; iproc < child.numChildren(); ++iproc)
567 const XMLObject& newChild = child.getChild(iproc);
570 if (newChild.hasAttribute(
"ID") &&
571 newChild.getRequiredInt(
"ID") == Comm_.MyPID())
573 for (
int j = 0; j < newChild.numContentLines(); ++j)
575 std::vector<std::string> tokens;
577 const std::string& line = newChild.getContentLine(j);
580 int tsize = (int) tokens.size();
581 for (
int k = 0; k < tsize; ++k)
585 #if defined(_MSC_VER)
586 MyGlobalElements[count++] = _strtoi64((
char*)tokens[k].c_str(), &endp, base);
589 std::istringstream ss(tokens[k]);
590 ss >> MyGlobalElements[count++];
592 MyGlobalElements[count++] = strtoll((
char*)tokens[k].c_str(), &endp, base);
600 Map =
new Epetra_Map(NumGlobalElements, NumMyElements,
601 &MyGlobalElements[0], IndexBase, Comm_);
610 Read(
const std::string& Label, std::vector<std::string>& Content)
612 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
613 "No file has been opened");
615 for (
int i = 0; i < fileXML_->numChildren(); ++i)
617 const XMLObject& child = fileXML_->getChild(i);
618 std::string tag = child.getTag();
622 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
624 for (
int j = 0; j < child.numContentLines(); ++j)
626 const std::string& line = child.getContentLine(j);
627 if (line ==
"\n")
continue;
628 Content.push_back(line);
637 Read(
const std::string& Label, Teuchos::ParameterList& List)
639 TEUCHOS_TEST_FOR_EXCEPTION(IsOpen_ ==
false, std::logic_error,
640 "No file has been opened");
642 for (
int i = 0; i < fileXML_->numChildren(); ++i)
644 const XMLObject& child = fileXML_->getChild(i);
645 std::string tag = child.getTag();
649 if (child.hasAttribute(
"Label") && child.getRequired(
"Label") == Label)
651 Teuchos::XMLParameterListReader ListReader;
652 List = ListReader.toParameterList(child.getChild(0));
void Read(const std::string &Label, Epetra_Map *&Map)
Reads the Epetra_Map stored with label Label.
int FillComplete(bool OptimizeDataStorage=true)
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
int InsertGlobalIndices(int GlobalRow, int NumIndices, int *Indices)
virtual int InsertGlobalValues(int GlobalRow, int NumEntries, const double *Values, const int *Indices)
void Read64(const std::string &Label, Epetra_Map *&Map)
Reads the Epetra_Map stored with label Label. Long Long version.