Rythmos - Transient Integration for Differential Equations  Version of the Day
 All Classes Functions Variables Typedefs Pages
Rythmos_DataStore_def.hpp
1 //@HEADER
2 // ***********************************************************************
3 //
4 // Rythmos Package
5 // Copyright (2006) 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_DATA_STORE_DEF_H
30 #define Rythmos_DATA_STORE_DEF_H
31 
32 #include "Rythmos_DataStore_decl.hpp"
33 
34 namespace Rythmos {
35 
36 // DataStore definitions:
37 template<class Scalar>
38 DataStore<Scalar>::DataStore()
39  :time(-1),
40  accuracy(-1)
41 {}
42 
43 template<class Scalar>
44 DataStore<Scalar>::DataStore(
45  Scalar &time_
46  ,const Teuchos::RCP<const Thyra::VectorBase<Scalar> > &x_
47  ,const Teuchos::RCP<const Thyra::VectorBase<Scalar> > &xdot_
48  ,ScalarMag &accuracy_)
49 {
50  time = time_;
51  x = x_;
52  xdot = xdot_;
53  accuracy = accuracy_;
54 }
55 
56 template<class Scalar>
57 DataStore<Scalar>::DataStore(
58  const DataStore<Scalar>& ds_in
59  )
60 {
61  time = ds_in.time;
62  x = ds_in.x;
63  xdot = ds_in.xdot;
64  accuracy = ds_in.accuracy;
65 }
66 
67 template<class Scalar>
68 RCP<DataStore<Scalar> > DataStore<Scalar>::clone() const
69 {
70  Scalar t_out = time;
71  RCP<VectorBase<Scalar> > x_out;
72  if (!Teuchos::is_null(x)) {
73  x_out = x->clone_v();
74  }
75  RCP<VectorBase<Scalar> > xdot_out;
76  if (!Teuchos::is_null(xdot)) {
77  xdot_out = xdot->clone_v();
78  }
79  ScalarMag accuracy_out = accuracy;
80  RCP<DataStore<Scalar> > ds_out = Teuchos::rcp(new DataStore<Scalar>(t_out,x_out,xdot_out,accuracy_out));
81  return ds_out;
82 }
83 
84 template<class Scalar>
85 bool DataStore<Scalar>::operator< (const DataStore<Scalar>& ds) const
86 {
87  return( this->time < ds.time );
88 }
89 
90 template<class Scalar>
91 bool DataStore<Scalar>::operator<= (const DataStore<Scalar>& ds) const
92 {
93  return( this->time <= ds.time );
94 }
95 
96 template<class Scalar>
97 bool DataStore<Scalar>::operator< (const Scalar& t) const
98 {
99  return( this->time < t );
100 }
101 
102 template<class Scalar>
103 bool DataStore<Scalar>::operator<= (const Scalar& t) const
104 {
105  return( this->time <= t );
106 }
107 
108 template<class Scalar>
109 bool DataStore<Scalar>::operator> (const DataStore<Scalar>& ds) const
110 {
111  return( this->time > ds.time );
112 }
113 
114 template<class Scalar>
115 bool DataStore<Scalar>::operator>= (const DataStore<Scalar>& ds) const
116 {
117  return( this->time >= ds.time );
118 }
119 
120 template<class Scalar>
121 bool DataStore<Scalar>::operator> (const Scalar& t) const
122 {
123  return( this->time > t );
124 }
125 
126 template<class Scalar>
127 bool DataStore<Scalar>::operator>= (const Scalar& t) const
128 {
129  return( this->time >= t );
130 }
131 
132 template<class Scalar>
133 bool DataStore<Scalar>::operator== (const DataStore<Scalar>& ds) const
134 {
135  return( this->time == ds.time );
136 }
137 
138 template<class Scalar>
139 bool DataStore<Scalar>::operator== (const Scalar& t) const
140 {
141  return( this->time == t );
142 }
143 
144 template<class Scalar>
145 std::string DataStore<Scalar>::description() const
146 {
147  std::string name = "Rythmos::DataStore";
148  return(name);
149 }
150 
151 template<class Scalar>
152 void DataStore<Scalar>::describe(
153  Teuchos::FancyOStream &out
154  ,const Teuchos::EVerbosityLevel verbLevel
155  ) const
156 {
157  if (verbLevel == Teuchos::VERB_EXTREME) {
158  out << description() << "::describe:" << std::endl;
159  out << "time = " << time << std::endl;
160  out << "x = " << std::endl;
161  x->describe(out,verbLevel);
162  if (xdot != Teuchos::null) {
163  out << "xdot = " << std::endl;
164  xdot->describe(out,verbLevel);
165  }
166  out << "accuracy = " << accuracy << std::endl;
167  }
168 }
169 
170 // DataStore Helper Function definitions:
171 template<class Scalar>
172 void dataStoreVectorToVector(
173  const typename DataStore<Scalar>::DataStoreVector_t &ds
174  ,Array<Scalar> *time_vec
175  ,Array<Teuchos::RCP<const Thyra::VectorBase<Scalar> > > *x_vec
176  ,Array<Teuchos::RCP<const Thyra::VectorBase<Scalar> > > *xdot_vec
177  ,Array<typename Teuchos::ScalarTraits<Scalar>::magnitudeType> *accuracy_vec)
178 {
179  if(time_vec)
180  time_vec->clear();
181  if(x_vec)
182  x_vec->clear();
183  if(xdot_vec)
184  xdot_vec->clear();
185  if(accuracy_vec)
186  accuracy_vec->clear();
187  int N = ds.size();
188  for (int i=0; i<N ; ++i) {
189  if(time_vec)
190  time_vec->push_back(ds[i].time);
191  if(x_vec)
192  x_vec->push_back(ds[i].x);
193  if(xdot_vec)
194  xdot_vec->push_back(ds[i].xdot);
195  if(accuracy_vec)
196  accuracy_vec->push_back(ds[i].accuracy);
197  }
198 }
199 
200 template<class Scalar>
201 void vectorToDataStoreVector(
202  const Array<Scalar> &time_vec
203  ,const Array<Teuchos::RCP<const Thyra::VectorBase<Scalar> > > &x_vec
204  ,const Array<Teuchos::RCP<const Thyra::VectorBase<Scalar> > > &xdot_vec
205  ,const Array<typename Teuchos::ScalarTraits<Scalar>::magnitudeType> &accuracy_vec
206  ,typename DataStore<Scalar>::DataStoreVector_t *ds
207  )
208 {
209  int N = time_vec.size();
210  int Nx = x_vec.size();
211  int Nxdot = xdot_vec.size();
212  int Nacc = accuracy_vec.size();
213  if ( (N != Nx) || (N != Nxdot) || (N != Nacc) ) {
214  ds = NULL;
215  return;
216  }
217  ds->clear();
218  for (int i=0; i<N ; ++i) {
219  Scalar time_temp = time_vec[i];
220  Teuchos::RCP<const Thyra::VectorBase<Scalar> > x_temp = x_vec[i];
221  Teuchos::RCP<const Thyra::VectorBase<Scalar> > xdot_temp = xdot_vec[i];
222  typename Teuchos::ScalarTraits<Scalar>::magnitudeType accuracy_temp = accuracy_vec[i];
223  DataStore<Scalar> ds_tmp(time_temp,x_temp,xdot_temp,accuracy_temp);
224  ds->push_back(ds_tmp);
225  }
226 }
227 
228 template<class Scalar>
229 void vectorToDataStoreList(
230  const Array<Scalar> &time_vec
231  ,const Array<Teuchos::RCP<const Thyra::VectorBase<Scalar> > > &x_vec
232  ,const Array<Teuchos::RCP<const Thyra::VectorBase<Scalar> > > &xdot_vec
233  ,const Array<typename Teuchos::ScalarTraits<Scalar>::magnitudeType> &accuracy_vec
234  ,typename DataStore<Scalar>::DataStoreList_t *ds)
235 {
236  int N = time_vec.size();
237  int Nx = x_vec.size();
238  int Nxdot = xdot_vec.size();
239  int Nacc = accuracy_vec.size();
240  if ( (N != Nx) || (N != Nxdot) || (N != Nacc) ) {
241  ds = NULL;
242  return;
243  }
244  ds->clear();
245  for (int i=0; i<N ; ++i) {
246  Scalar time_temp = time_vec[i];
247  Teuchos::RCP<const Thyra::VectorBase<Scalar> > x_temp = x_vec[i];
248  Teuchos::RCP<const Thyra::VectorBase<Scalar> > xdot_temp = xdot_vec[i];
249  typename Teuchos::ScalarTraits<Scalar>::magnitudeType accuracy_temp = accuracy_vec[i];
250  DataStore<Scalar> ds_tmp(time_temp,x_temp,xdot_temp,accuracy_temp);
251  ds->push_back(ds_tmp);
252  }
253 }
254 
255 template<class Scalar>
256 void vectorToDataStoreList(
257  const Array<Scalar> &time_vec
258  ,const Array<Teuchos::RCP<const Thyra::VectorBase<Scalar> > > &x_vec
259  ,const Array<Teuchos::RCP<const Thyra::VectorBase<Scalar> > > &xdot_vec
260  ,typename DataStore<Scalar>::DataStoreList_t *ds)
261 {
262  typedef Teuchos::ScalarTraits<Scalar> ST;
263  Array<typename Teuchos::ScalarTraits<Scalar>::magnitudeType> accuracy_vec;
264  int N = time_vec.size();
265  accuracy_vec.reserve(N);
266  for (int i=0 ; i<N ; ++i) {
267  accuracy_vec.push_back(ST::zero());
268  }
269  vectorToDataStoreList(time_vec,x_vec,xdot_vec,accuracy_vec,ds);
270 }
271 
272 //
273 // Explicit Instantiation macro
274 //
275 // Must be expanded from within the Rythmos namespace!
276 //
277 
278 #define RYTHMOS_DATA_STORE_INSTANT(SCALAR) \
279  \
280  template class DataStore< SCALAR >; \
281  \
282  template void dataStoreVectorToVector( \
283  const DataStore< SCALAR >::DataStoreVector_t &ds \
284  ,Array< SCALAR > *time_vec \
285  ,Array<Teuchos::RCP<const Thyra::VectorBase< SCALAR > > > *x_vec \
286  ,Array<Teuchos::RCP<const Thyra::VectorBase< SCALAR > > > *xdot_vec \
287  ,Array<Teuchos::ScalarTraits< SCALAR >::magnitudeType> *accuracy_vec \
288  ); \
289  \
290  template void vectorToDataStoreVector( \
291  const Array< SCALAR > &time_vec \
292  ,const Array<Teuchos::RCP<const Thyra::VectorBase< SCALAR > > > &x_vec \
293  ,const Array<Teuchos::RCP<const Thyra::VectorBase< SCALAR > > > &xdot_vec \
294  ,const Array<Teuchos::ScalarTraits< SCALAR >::magnitudeType> &accuracy_vec \
295  ,DataStore< SCALAR >::DataStoreVector_t *ds \
296  ); \
297  \
298  template void vectorToDataStoreList( \
299  const Array< SCALAR > &time_vec \
300  ,const Array<Teuchos::RCP<const Thyra::VectorBase< SCALAR > > > &x_vec \
301  ,const Array<Teuchos::RCP<const Thyra::VectorBase< SCALAR > > > &xdot_vec \
302  ,const Array<Teuchos::ScalarTraits< SCALAR >::magnitudeType> &accuracy_vec \
303  ,DataStore< SCALAR >::DataStoreList_t *ds \
304  ); \
305  \
306  template void vectorToDataStoreList( \
307  const Array< SCALAR > &time_vec \
308  ,const Array<Teuchos::RCP<const Thyra::VectorBase< SCALAR > > > &x_vec \
309  ,const Array<Teuchos::RCP<const Thyra::VectorBase< SCALAR > > > &xdot_vec \
310  ,DataStore< SCALAR >::DataStoreList_t *ds \
311  );
312 
313 } // namespace Rythmos
314 
315 #endif // Rythmos_DATA_STORE_DEF_H
316