Rythmos - Transient Integration for Differential Equations Version of the Day
Rythmos_StateSerializerStrategy.hpp
00001 //@HEADER
00002 // ***********************************************************************
00003 //
00004 //                     Rythmos Package
00005 //                 Copyright (2009) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Todd S. Coffey (tscoffe@sandia.gov)
00025 //
00026 // ***********************************************************************
00027 //@HEADER
00028 
00029 #ifndef Rythmos_STATE_SERIALIZER_STRATEGY_H
00030 #define Rythmos_STATE_SERIALIZER_STRATEGY_H
00031 
00032 #include "Rythmos_Types.hpp"
00033 
00034 #include "Teuchos_Describable.hpp"
00035 
00036 #include "Thyra_VectorBase.hpp"
00037 #include "Thyra_ModelEvaluatorBase.hpp"
00038 #include "Teuchos_ParameterList.hpp"
00039 #include "Teuchos_XMLParser.hpp"
00040 #include "Teuchos_XMLParameterListWriter.hpp"
00041 #include "Teuchos_XMLParameterListReader.hpp"
00042 #include "Teuchos_StringInputSource.hpp"
00043 #include "Thyra_SpmdMultiVectorSerializer.hpp"
00044 #include <string>
00045 
00046 namespace Rythmos {
00047 
00048 
00052 template<class Scalar> 
00053 class StateSerializerStrategy
00054   : virtual public Teuchos::Describable
00055 {
00056 public:
00057 
00058   virtual void serializeScalar(const Scalar& s, std::ostream& oStream) const = 0;
00059   virtual void deSerializeScalar(const Ptr<Scalar>& s, std::istream& iStream) const = 0;
00060 
00061   virtual void serializeInt(const int& i, std::ostream& oStream) const = 0;
00062   virtual void deSerializeInt(const Ptr<int>& i, std::istream& iStream) const = 0;
00063 
00064   virtual void serializeBool(const bool& b, std::ostream& oStream) const = 0;
00065   virtual void deSerializeBool(const Ptr<bool>& b, std::istream& iStream) const = 0;
00066 
00067   virtual void serializeVectorBase(const VectorBase<Scalar>& vec, std::ostream& oStream) const = 0;
00068   virtual void deSerializeVectorBase(const Ptr<VectorBase<Scalar> >& vec, std::istream& iStream) const = 0;
00069 
00070   virtual void serializeParameterList(const Teuchos::ParameterList& pl, std::ostream& oStream) const = 0;
00071   virtual void deSerializeParameterList(const Ptr<Teuchos::ParameterList>& pl, std::istream& iStream) const = 0;
00072 
00073 };
00074 
00075 
00076 template<class Scalar>
00077 class XMLStateSerializerStrategy
00078   : virtual public StateSerializerStrategy<Scalar>
00079 {
00080   public:
00081 
00082   XMLStateSerializerStrategy() {}
00083   virtual ~XMLStateSerializerStrategy() {}
00084 
00085   void serializeScalar(const Scalar& s, std::ostream& oStream) const
00086   {
00087     oStream.precision(std::numeric_limits<Scalar>::digits10+4);
00088     oStream << " " << s << " ";
00089   }
00090   void deSerializeScalar(const Ptr<Scalar>& s, std::istream& iStream) const
00091   {
00092     TEUCHOS_ASSERT( !Teuchos::is_null(s) );
00093     iStream >> (*s);
00094   }
00095 
00096   void serializeInt(const int& i, std::ostream& oStream) const 
00097   {
00098     oStream.precision(std::numeric_limits<Scalar>::digits10+4);
00099     oStream << " " << i << " ";
00100   }
00101   void deSerializeInt(const Ptr<int>& i, std::istream& iStream) const 
00102   {
00103     TEUCHOS_ASSERT( !Teuchos::is_null(i) );
00104     iStream >> (*i);
00105   }
00106 
00107   void serializeBool(const bool& b, std::ostream& oStream) const 
00108   {
00109     oStream.precision(std::numeric_limits<Scalar>::digits10+4);
00110     oStream << " " << b << " ";
00111   }
00112   void deSerializeBool(const Ptr<bool>& b, std::istream& iStream) const 
00113   {
00114     TEUCHOS_ASSERT( !Teuchos::is_null(b) );
00115     iStream >> (*b);
00116   }
00117 
00118   void serializeVectorBase(const VectorBase<Scalar>& vec, std::ostream& oStream) const 
00119   {
00120     Thyra::SpmdMultiVectorSerializer<double> vectorSerializer(false); // binaryMode = false
00121     vectorSerializer.serialize(vec, oStream);
00122   }
00123   void deSerializeVectorBase(const Ptr<VectorBase<Scalar> >& vec, std::istream& iStream) const 
00124   {
00125     TEUCHOS_ASSERT( !Teuchos::is_null(vec) );
00126     Thyra::SpmdMultiVectorSerializer<double> vectorSerializer(false); // binaryMode = false
00127     vectorSerializer.deserialize( iStream, vec.get() );
00128   }
00129 
00130   void serializeParameterList(const Teuchos::ParameterList& pl, std::ostream& oStream) const 
00131   {
00132     Teuchos::XMLParameterListWriter paramWriter;
00133     Teuchos::XMLObject XMLpl = paramWriter.toXML(pl);
00134     // Write special key to ostream to mark beginning of parameter list
00135     oStream << "\nRythmos::StateSerializerStrategy::serializeParameterList begin\n";
00136     oStream << XMLpl;
00137     // Write special key to ostream to mark end of parameter list
00138     oStream << "\nRythmos::StateSerializerStrategy::serializeParameterList end\n";
00139   }
00140   void deSerializeParameterList(const Ptr<Teuchos::ParameterList>& pl, std::istream& iStream) const 
00141   {
00142     TEUCHOS_ASSERT( !Teuchos::is_null(pl) );
00143     Teuchos::XMLObject XMLpl;
00144     std::ostringstream oStringStream;
00145     // Read in special key from istream to make sure this is a parameter list
00146     {
00147       std::string specialKey;
00148       while (specialKey != "Rythmos::StateSerializerStrategy::serializeParameterList begin" ) {
00149         std::getline(iStream,specialKey);
00150         TEUCHOS_ASSERT( !iStream.eof() );
00151       }
00152     }
00153     // Read until special key from istream is found that marks end of parameter list
00154     while (!iStream.eof()) {
00155       std::string line;
00156       std::getline(iStream,line);
00157       //std::cout << "line = >>" << line << "<<\n";
00158       if (line == "Rythmos::StateSerializerStrategy::serializeParameterList end") {
00159         break;
00160       }
00161       oStringStream << line;
00162     }
00163     Teuchos::StringInputSource src(oStringStream.str());
00164     Teuchos::XMLParser parser(src.stream());
00165     XMLpl = parser.parse();
00166     Teuchos::XMLParameterListReader paramReader;
00167     pl->setParameters(paramReader.toParameterList(XMLpl));
00168   }
00169 
00170 };
00171 
00172 template<class Scalar>
00173 class BinaryStateSerializerStrategy
00174   : virtual public StateSerializerStrategy<Scalar>
00175 {
00176   public:
00177 
00178   BinaryStateSerializerStrategy() {}
00179   virtual ~BinaryStateSerializerStrategy() {}
00180 
00181   void serializeScalar(const Scalar& s, std::ostream& oStream) const
00182   {
00183     oStream.precision(std::numeric_limits<Scalar>::digits10+4);
00184     oStream.write( reinterpret_cast<const char*>(&s), sizeof(Scalar) );
00185 
00186   }
00187   void deSerializeScalar(const Ptr<Scalar>& s, std::istream& iStream) const
00188   {
00189     TEUCHOS_ASSERT( !Teuchos::is_null(s) );
00190     iStream.read( reinterpret_cast<char*>(&*s), sizeof(Scalar) );
00191   }
00192 
00193   void serializeInt(const int& i, std::ostream& oStream) const 
00194   {
00195     oStream.precision(std::numeric_limits<Scalar>::digits10+4);
00196     oStream.write( reinterpret_cast<const char*>(&i), sizeof(int) );
00197   }
00198   void deSerializeInt(const Ptr<int>& i, std::istream& iStream) const 
00199   {
00200     TEUCHOS_ASSERT( !Teuchos::is_null(i) );
00201     iStream.read( reinterpret_cast<char*>(&*i), sizeof(int) );
00202   }
00203 
00204   void serializeBool(const bool& b, std::ostream& oStream) const 
00205   {
00206     oStream.precision(std::numeric_limits<Scalar>::digits10+4);
00207     oStream.write( reinterpret_cast<const char*>(&b), sizeof(bool) );
00208   }
00209   void deSerializeBool(const Ptr<bool>& b, std::istream& iStream) const 
00210   {
00211     TEUCHOS_ASSERT( !Teuchos::is_null(b) );
00212     iStream.read( reinterpret_cast<char*>(&*b), sizeof(bool) );
00213   }
00214 
00215   void serializeVectorBase(const VectorBase<Scalar>& vec, std::ostream& oStream) const 
00216   {
00217     Thyra::SpmdMultiVectorSerializer<double> vectorSerializer(true); // binaryMode = true
00218     vectorSerializer.serialize(vec, oStream);
00219   }
00220   void deSerializeVectorBase(const Ptr<VectorBase<Scalar> >& vec, std::istream& iStream) const 
00221   {
00222     TEUCHOS_ASSERT( !Teuchos::is_null(vec) );
00223     Thyra::SpmdMultiVectorSerializer<double> vectorSerializer(true); // binaryMode = true
00224     vectorSerializer.deserialize( iStream, vec.get() );
00225   }
00226 
00227   void serializeParameterList(const Teuchos::ParameterList& pl, std::ostream& oStream) const 
00228   {
00229     Teuchos::XMLParameterListWriter paramWriter;
00230     Teuchos::XMLObject XMLpl = paramWriter.toXML(pl);
00231     // Write special key to ostream to mark beginning of parameter list
00232     oStream << "\nRythmos::StateSerializerStrategy::serializeParameterList begin\n";
00233     oStream << XMLpl;
00234     // Write special key to ostream to mark end of parameter list
00235     oStream << "\nRythmos::StateSerializerStrategy::serializeParameterList end\n";
00236   }
00237   void deSerializeParameterList(const Ptr<Teuchos::ParameterList>& pl, std::istream& iStream) const 
00238   {
00239     TEUCHOS_ASSERT( !Teuchos::is_null(pl) );
00240     Teuchos::XMLObject XMLpl;
00241     std::ostringstream oStringStream;
00242     // Read in special key from istream to make sure this is a parameter list
00243     {
00244       std::string specialKey;
00245       while (specialKey != "Rythmos::StateSerializerStrategy::serializeParameterList begin" ) {
00246         std::getline(iStream,specialKey);
00247         TEUCHOS_ASSERT( !iStream.eof() );
00248       }
00249     }
00250     // Read until special key from istream is found that marks end of parameter list
00251     while (!iStream.eof()) {
00252       std::string line;
00253       std::getline(iStream,line);
00254       //std::cout << "line = >>" << line << "<<\n";
00255       if (line == "Rythmos::StateSerializerStrategy::serializeParameterList end") {
00256         break;
00257       }
00258       oStringStream << line;
00259     }
00260     Teuchos::StringInputSource src(oStringStream.str());
00261     Teuchos::XMLParser parser(src.stream());
00262     XMLpl = parser.parse();
00263     Teuchos::XMLParameterListReader paramReader;
00264     pl->setParameters(paramReader.toParameterList(XMLpl));
00265   }
00266 
00267 };
00268 
00269 } // namespace Rythmos
00270 #endif // Rythmos_STATE_SERIALIZER_STRATEGY_H
 All Classes Functions Variables Typedefs Friends