Sophie

Sophie

distrib > Mandriva > cooker > x86_64 > by-pkgid > 39c95c841fd4d04f01e6c15214514b4e > files > 56

lib64epetraext-devel-9.0.2-4mdv2011.0.x86_64.rpm

#ifndef EPETRAEXT_XMLWRITER_H
#define EPETRAEXT_XMLWRITER_H

#include "EpetraExt_ConfigDefs.h"
#include "Teuchos_RefCountPtr.hpp"
#include <fstream>

class Epetra_Map;
class Epetra_Comm;
class Epetra_Map;
class Epetra_MultiVector;
class Epetra_CrsGraph;
class Epetra_RowMatrix;
namespace Teuchos {
  class FileXML;
  class XMLObject;
  class ParameterList;
}

namespace EpetraExt 
{
/*! 
\brief class XMLWriter: A class for writing Trilinos objects to XML files.

Class EpetraExt::XMLWriter writes several Trilinos objects in an XML-compatible format.
The list of supported objects contains:
- Epetra_Map;
- Epetra_MultiVector;
- Epetra_CrsGraph;
- Epetra_CrsMatrix;
- Epetra_RowMatrix;
- Teuchos::ParameterList.

All objects can be read and written, with the std::exception of Epetra_RowMatrix
objects, that can only be written to files.

An example of usage is reported in file epetraext/example/inout/XML_IO.cpp.

Writing objects goes as follows. Let \c Map, \c Matrix, \c LHS and \c RHS an 
Epetra_Map, Epetra_CrsMatrix, and two Epetra_MultiVector's, respectively. First, we define an XMLWriter object
\code
EpetraExt::XMLWriter XMLWriter(Comm, "data.xml");
\endcode
and we open the file using \c MyProblem label:
\code
XMLWriter.Create("MyProblem");
\endcode
Writing objects simply goes as
\code
XMLWriter.Write("MyMap", Map);
XMLWriter.Write("MyMatrix", Matrix);
XMLWriter.Write("MyLHS", LHS);
XMLWriter.Write("MyRHS", RHS);
\endcode
A \c Teuchos::ParameterList (List), a \c std::string, and a \c std::vector<std::string> can be written as
\code
XMLWriter.Write("MyParameters", List);
XMLWriter.Write("Author", "myself and others");
XMLWriter.Write("Date", "May 2006");
\endcode
Finally, we close the file
\code
XMLWriter.Close();
\endcode
Note that only processor 0 writes the Teuchos::ParameterList, \c std::string, and \c std::vector<std::string>.

The written file is as follows:
\code
<ObjectCollection Label="MyProblem">
<Text Label="Author">
myself and others
</Text>
<Text Label="Date">
May 2006
</Text>
<Map Label="MyMap" NumElements="4" IndexBase="0" NumProc="1" ElementsOnProc0="4">
<Proc ID="0">
0
1
2
3
</Proc>
</Map>
<PointMatrix Label="MyMatrix" Rows="4" Columns="4" Nonzeros="4" Type="double" StartingIndex="0">
0 0 1
1 1 1
2 2 1
3 3 1
</PointMatrix>
<MultiVector Label="MyLHS" Length="4" NumVectors="2" Type="double">
-0.232996 -0.893077 
0.0388327 0.0594004 
0.661931 0.342299 
-0.930856 -0.984604 
</MultiVector>
<MultiVector Label="MyRHS" Length="4" NumVectors="2" Type="double">
0 0 
0 0 
0 0 
0 0 
</MultiVector>
<Text Label="MyContent">
This is an example of description
The description is as long as desired,
just put it in a std::vector of strings.
</Text>
<List Label="MyParameters">
<ParameterList>
<Parameter name="double parameter" type="double" value="10"/>
<Parameter name="int parameter" type="int" value="10"/>
<Parameter name="std::string parameter" type="std::string" value="std::string"/>
</ParameterList>
</List>
</ObjectCollection>
\endcode

This class requires Teuchos to be configured with the option \c --enable-teuchos-expat.

\author Marzio Sala, D-INFK/ETHZ

\date Last updated on 10-May-06.

*/
class XMLWriter
{
  public: 
    // @{ \name Constructor and destructor.
    //! ctor
    XMLWriter(const Epetra_Comm& Comm, const std::string& FileName); 

    //! dtor
    ~XMLWriter() {}

    //! Creates the file, giving \c Label to the whole object.
    void Create(const std::string& Label);

    //! Closes the file. No Write operations can follow.
    void Close();

    // @}
    // @{ \name Read operations
    
    //! Writes an Epetra_Map using label \c Label.
    void Write(const std::string& Label, const Epetra_Map& Map);

    //! Writes an Epetra_RowMatrix using label \c Label.
    void Write(const std::string& Label, const Epetra_RowMatrix& Matrix);

    //! Writes an Epetra_MultiVector using label \c Label.
    void Write(const std::string& Label, const Epetra_MultiVector& MultiVector);

    //! Writes the std::vector of std::string's using label \c Label.
    void Write(const std::string& Label, const std::vector<std::string>& Content);

    //! Writes input std::string using label \c Label.
    void Write(const std::string& Label, const std::string& Text)
    {
      std::vector<std::string> Content;
      Content.push_back(Text);
      Write(Label, Content);
    }

    //! Writes a Teuchos::ParameterList using label \c Label.
    void Write(const std::string& Label, Teuchos::ParameterList& List);

    // @}
  private:
    //! Epetra communicator.
    const Epetra_Comm& Comm_;
    //! Name of the file.
    std::string FileName_;
    //! If \c true, the file has been successfully opened.
    bool IsOpen_;
};

} // namespace EpetraExt

#endif