EpetraExt Package Browser (Single Doxygen Collection) Development
Loading...
Searching...
No Matches
XML_IO.cpp
Go to the documentation of this file.
1/*
2//@HEADER
3// ***********************************************************************
4//
5// EpetraExt: Epetra Extended - Linear Algebra Services Package
6// Copyright (2011) Sandia Corporation
7//
8// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9// the U.S. Government retains certain rights in this software.
10//
11// Redistribution and use in source and binary forms, with or without
12// modification, are permitted provided that the following conditions are
13// met:
14//
15// 1. Redistributions of source code must retain the above copyright
16// notice, this list of conditions and the following disclaimer.
17//
18// 2. Redistributions in binary form must reproduce the above copyright
19// notice, this list of conditions and the following disclaimer in the
20// documentation and/or other materials provided with the distribution.
21//
22// 3. Neither the name of the Corporation nor the names of the
23// contributors may be used to endorse or promote products derived from
24// this software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37//
38// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39//
40// ***********************************************************************
41//@HEADER
42*/
43
45#ifdef HAVE_MPI
46#include "mpi.h"
47#include "Epetra_MpiComm.h"
48#else
49#include "Epetra_SerialComm.h"
50#endif
51#include <vector>
52#include "Epetra_Map.h"
53#include "Epetra_CrsMatrix.h"
54#include "Epetra_MultiVector.h"
55#include "EpetraExt_XMLReader.h"
56#include "EpetraExt_XMLWriter.h"
57#include "Teuchos_ParameterList.hpp"
58
59// Showing the usage of XML I/O.
60// This example can be run with any number of processors.
61//
62// \author Marzio Sala, D-INFK/ETHZ.
63//
64// \date Last modified on 10-May-06.
65
66int main (int argc, char **argv)
67{
68#ifdef HAVE_MPI
69 MPI_Init(&argc, &argv);
70 Epetra_MpiComm Comm(MPI_COMM_WORLD);
71#else
73#endif
74
75 // define some Epetra objects
76
77 int n = Comm.NumProc() * 4;
78 Epetra_Map Map(n, 0, Comm);
79 Epetra_MultiVector x(Map, 2); x.Random();
80 Epetra_MultiVector b(Map, 2); x.Random();
81 Epetra_CrsMatrix Matrix(Copy, Map, 0);
82 // diagonal matrix
83 for (int i = 0; i < Map.NumMyElements(); ++i)
84 {
85 int ii = Map.GID(i);
86 double one = 1.0;
87 Matrix.InsertGlobalValues(ii, 1, &one, &ii);
88 }
89 Matrix.FillComplete();
90
91 Teuchos::ParameterList List;
92 List.set("int parameter", 10);
93 List.set("double parameter", 10.0);
94 List.set("std::string parameter", "std::string");
95
96 // ========================= //
97 // Part I: generate XML file //
98 // ========================= //
99
100 EpetraExt::XMLWriter XMLWriter(Comm, "data.xml");
101
102 std::vector<std::string> Content;
103 Content.push_back("This is an example of description");
104 Content.push_back("The description is as long as desired,");
105 Content.push_back("just put it in a std::vector of strings.");
106
107 XMLWriter.Create("MyProblem");
108 XMLWriter.Write("Author", "myself and others");
109 XMLWriter.Write("Date", "May 2006");
110 XMLWriter.Write("MyMap", Map);
111 XMLWriter.Write("MyMatrix", Matrix);
112 XMLWriter.Write("MyLHS", x);
113 XMLWriter.Write("MyRHS", b);
114 XMLWriter.Write("MyContent", Content);
115 XMLWriter.Write("MyParameters", List);
116 XMLWriter.Close();
117
118 // ================== //
119 // Part II: read data //
120 // ================== //
121
122 EpetraExt::XMLReader XMLReader(Comm, "data.xml");
123
124 Epetra_Map* MyMap;
125 Epetra_CrsMatrix* MyMatrix;
126 Epetra_MultiVector* MyLHS;
127 Epetra_MultiVector* MyRHS;
128 Teuchos::ParameterList MyParameters;
129 std::vector<std::string> Author;
130 std::vector<std::string> Date;
131 std::vector<std::string> MyContent;
132
133 XMLReader.Read("Author", Author);
134 XMLReader.Read("Date", Date);
135 XMLReader.Read("MyMap", MyMap);
136 XMLReader.Read("MyMatrix", MyMatrix);
137 XMLReader.Read("MyLHS", MyLHS);
138 XMLReader.Read("MyRHS", MyRHS);
139 XMLReader.Read("MyContent", MyContent);
140 XMLReader.Read("MyParameters", MyParameters);
141
142 std::cout << *MyMap;
143 std::cout << *MyMatrix;
144 std::cout << *MyLHS;
145 std::cout << *MyRHS;
146 if (Comm.MyPID() == 0)
147 {
148 int Msize = (int) MyContent.size();
149 for (int i = 0; i < Msize; ++i)
150 std::cout << MyContent[i] << std::endl;
151
152 std::cout << MyParameters;
153 std::cout << "Author = " << Author[0] << std::endl;
154 std::cout << "Date = " << Date[0] << std::endl;
155 }
156
157 delete MyMap;
158 delete MyMatrix;
159 delete MyLHS;
160 delete MyRHS;
161
162#ifdef HAVE_MPI
163 MPI_Finalize();
164#endif
165
166 return(EXIT_SUCCESS);
167}
Copy
int main(int argc, char **argv)
Definition: XML_IO.cpp:66
class XMLReader: A class for reading Epetra objects stored in XML files.
void Read(const std::string &Label, Epetra_Map *&Map)
Reads the Epetra_Map stored with label Label.
class XMLWriter: A class for writing Trilinos objects to XML files.
void Close()
Closes the file. No Write operations can follow.
void Write(const std::string &Label, const Epetra_Map &Map)
Writes an Epetra_Map using label Label.
void Create(const std::string &Label)
Creates the file, giving Label to the whole object.
int GID(int LID) const
int NumMyElements() const
int FillComplete(bool OptimizeDataStorage=true)
virtual int InsertGlobalValues(int GlobalRow, int NumEntries, const double *Values, const int *Indices)
int NumProc() const
int MyPID() const