Rythmos - Transient Integration for Differential Equations Version of the Day
Loading...
Searching...
No Matches
Rythmos_StateSerializerStrategy.hpp
1//@HEADER
2// ***********************************************************************
3//
4// Rythmos Package
5// Copyright (2009) Sandia Corporation
6//
7// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8// license for use of this work by or on behalf of the U.S. Government.
9//
10// This library is free software; you can redistribute it and/or modify
11// it under the terms of the GNU Lesser General Public License as
12// published by the Free Software Foundation; either version 2.1 of the
13// License, or (at your option) any later version.
14//
15// This library is distributed in the hope that it will be useful, but
16// WITHOUT ANY WARRANTY; without even the implied warranty of
17// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18// Lesser General Public License for more details.
19//
20// You should have received a copy of the GNU Lesser General Public
21// License along with this library; if not, write to the Free Software
22// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23// USA
24// Questions? Contact Todd S. Coffey (tscoffe@sandia.gov)
25//
26// ***********************************************************************
27//@HEADER
28
29#ifndef Rythmos_STATE_SERIALIZER_STRATEGY_H
30#define Rythmos_STATE_SERIALIZER_STRATEGY_H
31
32#include "Rythmos_Types.hpp"
33
34#include "Teuchos_Describable.hpp"
35
36#include "Thyra_VectorBase.hpp"
37#include "Thyra_ModelEvaluatorBase.hpp"
38#include "Teuchos_ParameterList.hpp"
39#include "Teuchos_XMLParser.hpp"
40#include "Teuchos_XMLParameterListWriter.hpp"
41#include "Teuchos_XMLParameterListReader.hpp"
42#include "Teuchos_StringInputSource.hpp"
43#include "Thyra_SpmdMultiVectorSerializer.hpp"
44#include <string>
45
46namespace Rythmos {
47
48
52template<class Scalar>
54 : virtual public Teuchos::Describable
55{
56public:
57
58 virtual void serializeScalar(const Scalar& s, std::ostream& oStream) const = 0;
59 virtual void deSerializeScalar(const Ptr<Scalar>& s, std::istream& iStream) const = 0;
60
61 virtual void serializeInt(const int& i, std::ostream& oStream) const = 0;
62 virtual void deSerializeInt(const Ptr<int>& i, std::istream& iStream) const = 0;
63
64 virtual void serializeBool(const bool& b, std::ostream& oStream) const = 0;
65 virtual void deSerializeBool(const Ptr<bool>& b, std::istream& iStream) const = 0;
66
67 virtual void serializeVectorBase(const VectorBase<Scalar>& vec, std::ostream& oStream) const = 0;
68 virtual void deSerializeVectorBase(const Ptr<VectorBase<Scalar> >& vec, std::istream& iStream) const = 0;
69
70 virtual void serializeParameterList(const Teuchos::ParameterList& pl, std::ostream& oStream) const = 0;
71 virtual void deSerializeParameterList(const Ptr<Teuchos::ParameterList>& pl, std::istream& iStream) const = 0;
72
73};
74
75
76template<class Scalar>
77class XMLStateSerializerStrategy
78 : virtual public StateSerializerStrategy<Scalar>
79{
80 public:
81
82 XMLStateSerializerStrategy() {}
83 virtual ~XMLStateSerializerStrategy() {}
84
85 void serializeScalar(const Scalar& s, std::ostream& oStream) const
86 {
87 oStream.precision(std::numeric_limits<Scalar>::digits10+4);
88 oStream << " " << s << " ";
89 }
90 void deSerializeScalar(const Ptr<Scalar>& s, std::istream& iStream) const
91 {
92 TEUCHOS_ASSERT( !Teuchos::is_null(s) );
93 iStream >> (*s);
94 }
95
96 void serializeInt(const int& i, std::ostream& oStream) const
97 {
98 oStream.precision(std::numeric_limits<Scalar>::digits10+4);
99 oStream << " " << i << " ";
100 }
101 void deSerializeInt(const Ptr<int>& i, std::istream& iStream) const
102 {
103 TEUCHOS_ASSERT( !Teuchos::is_null(i) );
104 iStream >> (*i);
105 }
106
107 void serializeBool(const bool& b, std::ostream& oStream) const
108 {
109 oStream.precision(std::numeric_limits<Scalar>::digits10+4);
110 oStream << " " << b << " ";
111 }
112 void deSerializeBool(const Ptr<bool>& b, std::istream& iStream) const
113 {
114 TEUCHOS_ASSERT( !Teuchos::is_null(b) );
115 iStream >> (*b);
116 }
117
118 void serializeVectorBase(const VectorBase<Scalar>& vec, std::ostream& oStream) const
119 {
120 Thyra::SpmdMultiVectorSerializer<double> vectorSerializer(false); // binaryMode = false
121 vectorSerializer.serialize(vec, oStream);
122 }
123 void deSerializeVectorBase(const Ptr<VectorBase<Scalar> >& vec, std::istream& iStream) const
124 {
125 TEUCHOS_ASSERT( !Teuchos::is_null(vec) );
126 Thyra::SpmdMultiVectorSerializer<double> vectorSerializer(false); // binaryMode = false
127 vectorSerializer.deserialize( iStream, vec.get() );
128 }
129
130 void serializeParameterList(const Teuchos::ParameterList& pl, std::ostream& oStream) const
131 {
132 Teuchos::XMLParameterListWriter paramWriter;
133 Teuchos::XMLObject XMLpl = paramWriter.toXML(pl);
134 // Write special key to ostream to mark beginning of parameter list
135 oStream << "\nRythmos::StateSerializerStrategy::serializeParameterList begin\n";
136 oStream << XMLpl;
137 // Write special key to ostream to mark end of parameter list
138 oStream << "\nRythmos::StateSerializerStrategy::serializeParameterList end\n";
139 }
140 void deSerializeParameterList(const Ptr<Teuchos::ParameterList>& pl, std::istream& iStream) const
141 {
142 TEUCHOS_ASSERT( !Teuchos::is_null(pl) );
143 Teuchos::XMLObject XMLpl;
144 std::ostringstream oStringStream;
145 // Read in special key from istream to make sure this is a parameter list
146 {
147 std::string specialKey;
148 while (specialKey != "Rythmos::StateSerializerStrategy::serializeParameterList begin" ) {
149 std::getline(iStream,specialKey);
150 TEUCHOS_ASSERT( !iStream.eof() );
151 }
152 }
153 // Read until special key from istream is found that marks end of parameter list
154 while (!iStream.eof()) {
155 std::string line;
156 std::getline(iStream,line);
157 //std::cout << "line = >>" << line << "<<\n";
158 if (line == "Rythmos::StateSerializerStrategy::serializeParameterList end") {
159 break;
160 }
161 oStringStream << line;
162 }
163 Teuchos::StringInputSource src(oStringStream.str());
164 Teuchos::XMLParser parser(src.stream());
165 XMLpl = parser.parse();
166 Teuchos::XMLParameterListReader paramReader;
167 pl->setParameters(paramReader.toParameterList(XMLpl));
168 }
169
170};
171
172template<class Scalar>
173class BinaryStateSerializerStrategy
174 : virtual public StateSerializerStrategy<Scalar>
175{
176 public:
177
178 BinaryStateSerializerStrategy() {}
179 virtual ~BinaryStateSerializerStrategy() {}
180
181 void serializeScalar(const Scalar& s, std::ostream& oStream) const
182 {
183 oStream.precision(std::numeric_limits<Scalar>::digits10+4);
184 oStream.write( reinterpret_cast<const char*>(&s), sizeof(Scalar) );
185
186 }
187 void deSerializeScalar(const Ptr<Scalar>& s, std::istream& iStream) const
188 {
189 TEUCHOS_ASSERT( !Teuchos::is_null(s) );
190 iStream.read( reinterpret_cast<char*>(&*s), sizeof(Scalar) );
191 }
192
193 void serializeInt(const int& i, std::ostream& oStream) const
194 {
195 oStream.precision(std::numeric_limits<Scalar>::digits10+4);
196 oStream.write( reinterpret_cast<const char*>(&i), sizeof(int) );
197 }
198 void deSerializeInt(const Ptr<int>& i, std::istream& iStream) const
199 {
200 TEUCHOS_ASSERT( !Teuchos::is_null(i) );
201 iStream.read( reinterpret_cast<char*>(&*i), sizeof(int) );
202 }
203
204 void serializeBool(const bool& b, std::ostream& oStream) const
205 {
206 oStream.precision(std::numeric_limits<Scalar>::digits10+4);
207 oStream.write( reinterpret_cast<const char*>(&b), sizeof(bool) );
208 }
209 void deSerializeBool(const Ptr<bool>& b, std::istream& iStream) const
210 {
211 TEUCHOS_ASSERT( !Teuchos::is_null(b) );
212 iStream.read( reinterpret_cast<char*>(&*b), sizeof(bool) );
213 }
214
215 void serializeVectorBase(const VectorBase<Scalar>& vec, std::ostream& oStream) const
216 {
217 Thyra::SpmdMultiVectorSerializer<double> vectorSerializer(true); // binaryMode = true
218 vectorSerializer.serialize(vec, oStream);
219 }
220 void deSerializeVectorBase(const Ptr<VectorBase<Scalar> >& vec, std::istream& iStream) const
221 {
222 TEUCHOS_ASSERT( !Teuchos::is_null(vec) );
223 Thyra::SpmdMultiVectorSerializer<double> vectorSerializer(true); // binaryMode = true
224 vectorSerializer.deserialize( iStream, vec.get() );
225 }
226
227 void serializeParameterList(const Teuchos::ParameterList& pl, std::ostream& oStream) const
228 {
229 Teuchos::XMLParameterListWriter paramWriter;
230 Teuchos::XMLObject XMLpl = paramWriter.toXML(pl);
231 // Write special key to ostream to mark beginning of parameter list
232 oStream << "\nRythmos::StateSerializerStrategy::serializeParameterList begin\n";
233 oStream << XMLpl;
234 // Write special key to ostream to mark end of parameter list
235 oStream << "\nRythmos::StateSerializerStrategy::serializeParameterList end\n";
236 }
237 void deSerializeParameterList(const Ptr<Teuchos::ParameterList>& pl, std::istream& iStream) const
238 {
239 TEUCHOS_ASSERT( !Teuchos::is_null(pl) );
240 Teuchos::XMLObject XMLpl;
241 std::ostringstream oStringStream;
242 // Read in special key from istream to make sure this is a parameter list
243 {
244 std::string specialKey;
245 while (specialKey != "Rythmos::StateSerializerStrategy::serializeParameterList begin" ) {
246 std::getline(iStream,specialKey);
247 TEUCHOS_ASSERT( !iStream.eof() );
248 }
249 }
250 // Read until special key from istream is found that marks end of parameter list
251 while (!iStream.eof()) {
252 std::string line;
253 std::getline(iStream,line);
254 //std::cout << "line = >>" << line << "<<\n";
255 if (line == "Rythmos::StateSerializerStrategy::serializeParameterList end") {
256 break;
257 }
258 oStringStream << line;
259 }
260 Teuchos::StringInputSource src(oStringStream.str());
261 Teuchos::XMLParser parser(src.stream());
262 XMLpl = parser.parse();
263 Teuchos::XMLParameterListReader paramReader;
264 pl->setParameters(paramReader.toParameterList(XMLpl));
265 }
266
267};
268
269} // namespace Rythmos
270#endif // Rythmos_STATE_SERIALIZER_STRATEGY_H
Base class for serializing Rythmos state data.