EpetraExt Package Browser (Single Doxygen Collection) Development
XML_IO.cpp
Go to the documentation of this file.
00001 #include "EpetraExt_ConfigDefs.h"
00002 #ifdef HAVE_MPI
00003 #include "mpi.h"
00004 #include "Epetra_MpiComm.h"
00005 #else
00006 #include "Epetra_SerialComm.h"
00007 #endif
00008 #include <vector>
00009 #include "Epetra_Map.h"
00010 #include "Epetra_CrsMatrix.h"
00011 #include "Epetra_MultiVector.h"
00012 #include "EpetraExt_XMLReader.h"
00013 #include "EpetraExt_XMLWriter.h"
00014 #include "Teuchos_ParameterList.hpp"
00015 
00016 // Showing the usage of XML I/O.
00017 // This example can be run with any number of processors.
00018 //
00019 // \author Marzio Sala, D-INFK/ETHZ.
00020 //
00021 // \date Last modified on 10-May-06.
00022 
00023 int main (int argc, char **argv)
00024 {
00025 #ifdef HAVE_MPI
00026   MPI_Init(&argc, &argv);
00027   Epetra_MpiComm Comm(MPI_COMM_WORLD);
00028 #else
00029   Epetra_SerialComm Comm;
00030 #endif
00031 
00032   // define some Epetra objects
00033 
00034   int n = Comm.NumProc() * 4;
00035   Epetra_Map Map(n, 0, Comm);
00036   Epetra_MultiVector x(Map, 2); x.Random();
00037   Epetra_MultiVector b(Map, 2); x.Random();
00038   Epetra_CrsMatrix Matrix(Copy, Map, 0);
00039   // diagonal matrix
00040   for (int i = 0; i < Map.NumMyElements(); ++i)
00041   {
00042     int ii = Map.GID(i);
00043     double one = 1.0;
00044     Matrix.InsertGlobalValues(ii, 1, &one, &ii);
00045   }
00046   Matrix.FillComplete();
00047 
00048   Teuchos::ParameterList List;
00049   List.set("int parameter", 10);
00050   List.set("double parameter", 10.0);
00051   List.set("std::string parameter", "std::string");
00052 
00053   // ========================= //
00054   // Part I: generate XML file //
00055   // ========================= //
00056   
00057   EpetraExt::XMLWriter XMLWriter(Comm, "data.xml");
00058 
00059   std::vector<std::string> Content;
00060   Content.push_back("This is an example of description");
00061   Content.push_back("The description is as long as desired,");
00062   Content.push_back("just put it in a std::vector of strings.");
00063 
00064   XMLWriter.Create("MyProblem");
00065   XMLWriter.Write("Author", "myself and others");
00066   XMLWriter.Write("Date", "May 2006");
00067   XMLWriter.Write("MyMap", Map);
00068   XMLWriter.Write("MyMatrix", Matrix);
00069   XMLWriter.Write("MyLHS", x);
00070   XMLWriter.Write("MyRHS", b);
00071   XMLWriter.Write("MyContent", Content);
00072   XMLWriter.Write("MyParameters", List);
00073   XMLWriter.Close();
00074 
00075   // ================== //
00076   // Part II: read data //
00077   // ================== //
00078   
00079   EpetraExt::XMLReader XMLReader(Comm, "data.xml");
00080 
00081   Epetra_Map* MyMap;
00082   Epetra_CrsMatrix* MyMatrix;
00083   Epetra_MultiVector* MyLHS;
00084   Epetra_MultiVector* MyRHS;
00085   Teuchos::ParameterList MyParameters;
00086   std::vector<std::string> Author;
00087   std::vector<std::string> Date;
00088   std::vector<std::string> MyContent;
00089 
00090   XMLReader.Read("Author", Author);
00091   XMLReader.Read("Date", Date);
00092   XMLReader.Read("MyMap", MyMap);
00093   XMLReader.Read("MyMatrix", MyMatrix);
00094   XMLReader.Read("MyLHS", MyLHS);
00095   XMLReader.Read("MyRHS", MyRHS);
00096   XMLReader.Read("MyContent", MyContent);
00097   XMLReader.Read("MyParameters", MyParameters);
00098 
00099   std::cout << *MyMap;
00100   std::cout << *MyMatrix;
00101   std::cout << *MyLHS;
00102   std::cout << *MyRHS;
00103   if (Comm.MyPID() == 0)
00104   {
00105     int Msize = (int) MyContent.size();
00106     for (int i = 0; i < Msize; ++i)
00107       std::cout << MyContent[i] << std::endl;
00108 
00109     std::cout << MyParameters;
00110     std::cout << "Author = " << Author[0] << std::endl;
00111     std::cout << "Date   = " << Date[0] << std::endl;
00112   }
00113 
00114   delete MyMap;
00115   delete MyMatrix;
00116   delete MyLHS;
00117   delete MyRHS;
00118 
00119 #ifdef HAVE_MPI
00120   MPI_Finalize();
00121 #endif
00122 
00123   return(EXIT_SUCCESS);
00124 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines