EpetraExt Package Browser (Single Doxygen Collection) Development
XML_IO.cpp
Go to the documentation of this file.
00001 /*
00002 //@HEADER
00003 // ***********************************************************************
00004 //
00005 //     EpetraExt: Epetra Extended - Linear Algebra Services Package
00006 //                 Copyright (2011) Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00039 //
00040 // ***********************************************************************
00041 //@HEADER
00042 */
00043 
00044 #include "EpetraExt_ConfigDefs.h"
00045 #ifdef HAVE_MPI
00046 #include "mpi.h"
00047 #include "Epetra_MpiComm.h"
00048 #else
00049 #include "Epetra_SerialComm.h"
00050 #endif
00051 #include <vector>
00052 #include "Epetra_Map.h"
00053 #include "Epetra_CrsMatrix.h"
00054 #include "Epetra_MultiVector.h"
00055 #include "EpetraExt_XMLReader.h"
00056 #include "EpetraExt_XMLWriter.h"
00057 #include "Teuchos_ParameterList.hpp"
00058 
00059 // Showing the usage of XML I/O.
00060 // This example can be run with any number of processors.
00061 //
00062 // \author Marzio Sala, D-INFK/ETHZ.
00063 //
00064 // \date Last modified on 10-May-06.
00065 
00066 int main (int argc, char **argv)
00067 {
00068 #ifdef HAVE_MPI
00069   MPI_Init(&argc, &argv);
00070   Epetra_MpiComm Comm(MPI_COMM_WORLD);
00071 #else
00072   Epetra_SerialComm Comm;
00073 #endif
00074 
00075   // define some Epetra objects
00076 
00077   int n = Comm.NumProc() * 4;
00078   Epetra_Map Map(n, 0, Comm);
00079   Epetra_MultiVector x(Map, 2); x.Random();
00080   Epetra_MultiVector b(Map, 2); x.Random();
00081   Epetra_CrsMatrix Matrix(Copy, Map, 0);
00082   // diagonal matrix
00083   for (int i = 0; i < Map.NumMyElements(); ++i)
00084   {
00085     int ii = Map.GID(i);
00086     double one = 1.0;
00087     Matrix.InsertGlobalValues(ii, 1, &one, &ii);
00088   }
00089   Matrix.FillComplete();
00090 
00091   Teuchos::ParameterList List;
00092   List.set("int parameter", 10);
00093   List.set("double parameter", 10.0);
00094   List.set("std::string parameter", "std::string");
00095 
00096   // ========================= //
00097   // Part I: generate XML file //
00098   // ========================= //
00099   
00100   EpetraExt::XMLWriter XMLWriter(Comm, "data.xml");
00101 
00102   std::vector<std::string> Content;
00103   Content.push_back("This is an example of description");
00104   Content.push_back("The description is as long as desired,");
00105   Content.push_back("just put it in a std::vector of strings.");
00106 
00107   XMLWriter.Create("MyProblem");
00108   XMLWriter.Write("Author", "myself and others");
00109   XMLWriter.Write("Date", "May 2006");
00110   XMLWriter.Write("MyMap", Map);
00111   XMLWriter.Write("MyMatrix", Matrix);
00112   XMLWriter.Write("MyLHS", x);
00113   XMLWriter.Write("MyRHS", b);
00114   XMLWriter.Write("MyContent", Content);
00115   XMLWriter.Write("MyParameters", List);
00116   XMLWriter.Close();
00117 
00118   // ================== //
00119   // Part II: read data //
00120   // ================== //
00121   
00122   EpetraExt::XMLReader XMLReader(Comm, "data.xml");
00123 
00124   Epetra_Map* MyMap;
00125   Epetra_CrsMatrix* MyMatrix;
00126   Epetra_MultiVector* MyLHS;
00127   Epetra_MultiVector* MyRHS;
00128   Teuchos::ParameterList MyParameters;
00129   std::vector<std::string> Author;
00130   std::vector<std::string> Date;
00131   std::vector<std::string> MyContent;
00132 
00133   XMLReader.Read("Author", Author);
00134   XMLReader.Read("Date", Date);
00135   XMLReader.Read("MyMap", MyMap);
00136   XMLReader.Read("MyMatrix", MyMatrix);
00137   XMLReader.Read("MyLHS", MyLHS);
00138   XMLReader.Read("MyRHS", MyRHS);
00139   XMLReader.Read("MyContent", MyContent);
00140   XMLReader.Read("MyParameters", MyParameters);
00141 
00142   std::cout << *MyMap;
00143   std::cout << *MyMatrix;
00144   std::cout << *MyLHS;
00145   std::cout << *MyRHS;
00146   if (Comm.MyPID() == 0)
00147   {
00148     int Msize = (int) MyContent.size();
00149     for (int i = 0; i < Msize; ++i)
00150       std::cout << MyContent[i] << std::endl;
00151 
00152     std::cout << MyParameters;
00153     std::cout << "Author = " << Author[0] << std::endl;
00154     std::cout << "Date   = " << Date[0] << std::endl;
00155   }
00156 
00157   delete MyMap;
00158   delete MyMatrix;
00159   delete MyLHS;
00160   delete MyRHS;
00161 
00162 #ifdef HAVE_MPI
00163   MPI_Finalize();
00164 #endif
00165 
00166   return(EXIT_SUCCESS);
00167 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines