Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_SolutionState_impl.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ****************************************************************************
3 // Tempus: Copyright (2017) Sandia Corporation
4 //
5 // Distributed under BSD 3-clause license (See accompanying file Copyright.txt)
6 // ****************************************************************************
7 // @HEADER
8 
9 #ifndef Tempus_SolutionState_impl_hpp
10 #define Tempus_SolutionState_impl_hpp
11 
12 #include "Thyra_VectorStdOps.hpp"
13 
14 namespace Tempus {
15 
16 template <class Scalar>
18  : x_(Teuchos::null),
19  x_nc_(Teuchos::null),
20  xdot_(Teuchos::null),
21  xdot_nc_(Teuchos::null),
22  xdotdot_(Teuchos::null),
23  xdotdot_nc_(Teuchos::null),
24  stepperState_(Teuchos::null),
25  stepperState_nc_(Teuchos::null),
26  physicsState_(Teuchos::null),
27  physicsState_nc_(Teuchos::null)
28 {
35 }
36 
37 template <class Scalar>
42  const Teuchos::RCP<Thyra::VectorBase<Scalar> >& xdotdot,
43  const Teuchos::RCP<StepperState<Scalar> >& stepperState,
44  const Teuchos::RCP<PhysicsState<Scalar> >& physicsState)
45  : metaData_(metaData),
46  metaData_nc_(metaData),
47  x_(x),
48  x_nc_(x),
49  xdot_(xdot),
50  xdot_nc_(xdot),
51  xdotdot_(xdotdot),
52  xdotdot_nc_(xdotdot),
53  stepperState_(stepperState),
54  stepperState_nc_(stepperState),
55  physicsState_(physicsState),
56  physicsState_nc_(physicsState)
57 {
58  if (stepperState_nc_ == Teuchos::null) {
61  }
62  if (physicsState_nc_ == Teuchos::null) {
65  }
66 }
67 
68 template <class Scalar>
70  const Teuchos::RCP<const SolutionStateMetaData<Scalar> > metaData,
71  const Teuchos::RCP<const Thyra::VectorBase<Scalar> >& x,
72  const Teuchos::RCP<const Thyra::VectorBase<Scalar> >& xdot,
73  const Teuchos::RCP<const Thyra::VectorBase<Scalar> >& xdotdot,
74  const Teuchos::RCP<const StepperState<Scalar> >& stepperState,
75  const Teuchos::RCP<const PhysicsState<Scalar> >& physicsState)
76  : metaData_(metaData),
77  metaData_nc_(Teuchos::null),
78  x_(x),
79  x_nc_(Teuchos::null),
80  xdot_(xdot),
81  xdot_nc_(Teuchos::null),
82  xdotdot_(xdotdot),
83  xdotdot_nc_(Teuchos::null),
84  stepperState_(stepperState),
85  stepperState_nc_(Teuchos::null),
86  physicsState_(physicsState),
87  physicsState_nc_(Teuchos::null)
88 {
89  if (stepperState_ == Teuchos::null) {
91  }
92  if (physicsState_ == Teuchos::null) {
94  }
95 }
96 
97 template <class Scalar>
99  : metaData_(ss_.metaData_),
100  metaData_nc_(ss_.metaData_nc_),
101  x_(ss_.x_),
102  x_nc_(ss_.x_nc_),
103  xdot_(ss_.xdot_),
104  xdot_nc_(ss_.xdot_nc_),
105  xdotdot_(ss_.xdotdot_),
106  xdotdot_nc_(ss_.xdotdot_nc_),
107  stepperState_(ss_.stepperState_),
108  stepperState_nc_(ss_.stepperState_nc_),
109  physicsState_(ss_.physicsState_),
110  physicsState_nc_(ss_.physicsState_nc_)
111 {
112 }
113 
114 template <class Scalar>
116 {
117  using Teuchos::RCP;
118 
119  RCP<SolutionStateMetaData<Scalar> > metaData_out;
120  if (!Teuchos::is_null(metaData_)) metaData_out = metaData_->clone();
121 
122  RCP<Thyra::VectorBase<Scalar> > x_out;
123  if (!Teuchos::is_null(x_)) x_out = x_->clone_v();
124 
125  RCP<Thyra::VectorBase<Scalar> > xdot_out;
126  if (!Teuchos::is_null(xdot_)) xdot_out = xdot_->clone_v();
127 
128  RCP<Thyra::VectorBase<Scalar> > xdotdot_out;
129  if (!Teuchos::is_null(xdotdot_)) xdotdot_out = xdotdot_->clone_v();
130 
131  RCP<StepperState<Scalar> > sS_out;
132  if (!Teuchos::is_null(stepperState_)) sS_out = stepperState_->clone();
133 
134  RCP<PhysicsState<Scalar> > pS_out;
135  if (!Teuchos::is_null(physicsState_)) pS_out = physicsState_->clone();
136 
137  RCP<SolutionState<Scalar> > ss_out = Teuchos::rcp(new SolutionState<Scalar>(
138  metaData_out, x_out, xdot_out, xdotdot_out, sS_out, pS_out));
139 
140  return ss_out;
141 }
142 
143 template <class Scalar>
145  const Teuchos::RCP<const SolutionState<Scalar> >& ss)
146 {
147  metaData_nc_->copy(ss->metaData_);
148  this->copySolutionData(ss);
149 }
150 
151 template <class Scalar>
153  const Teuchos::RCP<const SolutionState<Scalar> >& ss)
154 {
155  if (ss->x_ == Teuchos::null)
156  x_nc_ = Teuchos::null;
157  else {
158  if (x_nc_ == Teuchos::null) {
159  x_nc_ = ss->x_->clone_v();
160  }
161  else
162  Thyra::V_V(x_nc_.ptr(), *(ss->x_));
163  }
164  x_ = x_nc_;
165 
166  if (ss->xdot_ == Teuchos::null)
167  xdot_nc_ = Teuchos::null;
168  else {
169  if (xdot_nc_ == Teuchos::null)
170  xdot_nc_ = ss->xdot_->clone_v();
171  else
172  Thyra::V_V(xdot_nc_.ptr(), *(ss->xdot_));
173  }
174  xdot_ = xdot_nc_;
175 
176  if (ss->xdotdot_ == Teuchos::null)
177  xdotdot_nc_ = Teuchos::null;
178  else {
179  if (xdotdot_nc_ == Teuchos::null)
180  xdotdot_nc_ = ss->xdotdot_->clone_v();
181  else
182  Thyra::V_V(xdotdot_nc_.ptr(), *(ss->xdotdot_));
183  }
184  xdotdot_ = xdotdot_nc_;
185 
186  if (ss->stepperState_ == Teuchos::null)
187  stepperState_nc_ = Teuchos::null;
188  else {
189  if (stepperState_nc_ == Teuchos::null)
190  stepperState_nc_ = ss->stepperState_->clone();
191  else
192  stepperState_nc_->copy(ss->stepperState_);
193  }
194  stepperState_ = stepperState_nc_;
195 
196  if (ss->physicsState_ == Teuchos::null)
197  physicsState_nc_ = Teuchos::null;
198  else {
199  if (physicsState_nc_ == Teuchos::null)
200  physicsState_nc_ = ss->physicsState_->clone();
201  else
202  physicsState_nc_->copy(ss->physicsState_);
203  }
204  physicsState_ = physicsState_nc_;
205 }
206 
207 template <class Scalar>
209 {
210  return (this->metaData_->getTime() < ss.metaData_->getTime());
211 }
212 
213 template <class Scalar>
215 {
216  return (this->metaData_->getTime() <= ss.metaData_->getTime());
217 }
218 
219 template <class Scalar>
220 bool SolutionState<Scalar>::operator<(const Scalar& t) const
221 {
222  return (this->metaData_->getTime() < t);
223 }
224 
225 template <class Scalar>
226 bool SolutionState<Scalar>::operator<=(const Scalar& t) const
227 {
228  return (this->metaData_->getTime() <= t);
229 }
230 
231 template <class Scalar>
233 {
234  return (this->metaData_->getTime() > ss.metaData_->getTime());
235 }
236 
237 template <class Scalar>
239 {
240  return (this->metaData_->getTime() >= ss.metaData_->getTime());
241 }
242 
243 template <class Scalar>
244 bool SolutionState<Scalar>::operator>(const Scalar& t) const
245 {
246  return (this->metaData_->getTime() > t);
247 }
248 
249 template <class Scalar>
250 bool SolutionState<Scalar>::operator>=(const Scalar& t) const
251 {
252  return (this->metaData_->getTime() >= t);
253 }
254 
255 template <class Scalar>
257 {
258  return (this->metaData_->getTime() == ss.metaData_->getTime());
259 }
260 
261 template <class Scalar>
262 bool SolutionState<Scalar>::operator==(const Scalar& t) const
263 {
264  return (this->metaData_->getTime() == t);
265 }
266 
267 template <class Scalar>
269 {
270  std::ostringstream out;
271  out << "SolutionState"
272  << " (index =" << std::setw(6) << this->getIndex()
273  << "; time =" << std::setw(10) << std::setprecision(3) << this->getTime()
274  << "; dt =" << std::setw(10) << std::setprecision(3)
275  << this->getTimeStep() << ")";
276  return out.str();
277 }
278 
279 template <class Scalar>
281  Teuchos::FancyOStream& out, const Teuchos::EVerbosityLevel verbLevel) const
282 {
283  auto l_out = Teuchos::fancyOStream(out.getOStream());
284  Teuchos::OSTab ostab(*l_out, 2, this->description());
285  l_out->setOutputToRootOnly(0);
286 
287  *l_out << "\n--- " << this->description() << " ---" << std::endl;
288 
289  if (Teuchos::as<int>(verbLevel) >= Teuchos::as<int>(Teuchos::VERB_EXTREME)) {
290  metaData_->describe(*l_out, verbLevel);
291  *l_out << " x = " << std::endl;
292  x_->describe(*l_out, verbLevel);
293 
294  if (xdot_ != Teuchos::null) {
295  *l_out << " xdot_ = " << std::endl;
296  xdot_->describe(*l_out, verbLevel);
297  }
298  if (xdotdot_ != Teuchos::null) {
299  *l_out << " xdotdot = " << std::endl;
300  xdotdot_->describe(*l_out, verbLevel);
301  }
302 
303  if (stepperState_ != Teuchos::null)
304  stepperState_->describe(*l_out, verbLevel);
305  if (physicsState_ != Teuchos::null)
306  physicsState_->describe(*l_out, verbLevel);
307 
308  *l_out << std::string(this->description().length() + 8, '-') << std::endl;
309  }
310 }
311 
312 template <class Scalar>
314  const Teuchos::RCP<const SolutionState<Scalar> >& ssIn)
315 {
316  if (!getComputeNorms()) return;
317 
318  auto x = this->getX();
319  this->setXNormL2(Thyra::norm(*x));
320 
321  if (ssIn != Teuchos::null) {
322  auto xIn = ssIn->getX();
323 
324  // dx = x - xIn
326  Thyra::createMember(x->space());
327  Thyra::V_VmV(dx.ptr(), *x, *xIn);
328  Scalar dxNorm = Thyra::norm(*dx);
329  Scalar xInNorm = Thyra::norm(*xIn);
330  this->setDxNormL2Abs(dxNorm);
331  // Compute change, e.g., ||x^n-x^(n-1)||/||x^(n-1)||
332  const Scalar eps = std::numeric_limits<Scalar>::epsilon();
333  const Scalar min = std::numeric_limits<Scalar>::min();
334  if (xInNorm < min / eps) { // numerically zero
335  this->setDxNormL2Rel(std::numeric_limits<Scalar>::infinity());
336  }
337  else {
338  // this->setDxNormL2Rel(dxNorm/(xInNorm + eps));
339  this->setDxNormL2Rel(dxNorm / (xInNorm * (1.0 + 1.0e4 * eps)));
340  }
341  }
342 }
343 
344 // Nonmember constructors.
345 // ------------------------------------------------------------------------
346 
347 template <class Scalar>
351  const Teuchos::RCP<Thyra::VectorBase<Scalar> >& xdotdot)
352 {
355 
356  Teuchos::RCP<StepperState<Scalar> > stepperState_nc =
357  Teuchos::rcp(new StepperState<Scalar>("Default"));
358 
359  Teuchos::RCP<PhysicsState<Scalar> > physicsState_nc =
361 
363  metaData_nc, x, xdot, xdotdot, stepperState_nc, physicsState_nc));
364 
365  return ss;
366 }
367 
368 template <class Scalar>
370  const Teuchos::RCP<const Thyra::VectorBase<Scalar> >& x,
371  const Teuchos::RCP<const Thyra::VectorBase<Scalar> >& xdot,
372  const Teuchos::RCP<const Thyra::VectorBase<Scalar> >& xdotdot)
373 {
376 
378  Teuchos::rcp(new StepperState<Scalar>("Default"));
379 
382 
384  metaData, x, xdot, xdotdot, stepperState, physicsState));
385 
386  return ss;
387 }
388 
389 template <class Scalar>
391  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& model,
392  const Teuchos::RCP<StepperState<Scalar> >& stepperState,
393  const Teuchos::RCP<PhysicsState<Scalar> >& physicsState)
394 {
395  typedef Thyra::ModelEvaluatorBase MEB;
396  using Teuchos::rcp_const_cast;
397 
398  auto metaData_nc = Teuchos::rcp(new SolutionStateMetaData<Scalar>());
399  metaData_nc->setSolutionStatus(Status::PASSED);
400 
401  MEB::InArgs<Scalar> inArgs = model->getNominalValues();
402 
404  inArgs.supports(MEB::IN_ARG_x) == false, std::logic_error,
405  model->description() << "does not support an x solution vector!");
406 
407  // The solution vector, x, is required (usually).
408  auto x_nc = rcp_const_cast<Thyra::VectorBase<Scalar> >(inArgs.get_x());
409 
410  // The solution derivative, xdot, can be optional provided, based on
411  // application needs. Here we will base it on "supports" IN_ARG_x_dot.
412  // Depending on the stepper used, a temporary xdot vector may be created
413  // within the Stepper, but not moved to the SolutionState.
415  if (inArgs.supports(MEB::IN_ARG_x_dot)) {
416  xdot_nc = rcp_const_cast<Thyra::VectorBase<Scalar> >(inArgs.get_x_dot());
417  }
418  else {
419  xdot_nc = Teuchos::null;
420  }
421 
422  // Similar as xdot.
424  if (inArgs.supports(MEB::IN_ARG_x_dot_dot)) {
425  xdotdot_nc =
426  rcp_const_cast<Thyra::VectorBase<Scalar> >(inArgs.get_x_dot_dot());
427  }
428  else {
429  xdotdot_nc = Teuchos::null;
430  }
431 
432  Teuchos::RCP<StepperState<Scalar> > stepperState_nc;
433  if (stepperState == Teuchos::null) {
434  stepperState_nc = Teuchos::rcp(new StepperState<Scalar>()); // Use default
435  }
436  else {
437  stepperState_nc = stepperState;
438  }
439 
440  Teuchos::RCP<PhysicsState<Scalar> > physicsState_nc;
441  if (physicsState == Teuchos::null) {
442  physicsState_nc = Teuchos::rcp(new PhysicsState<Scalar>()); // Use default
443  }
444  else {
445  physicsState_nc = physicsState;
446  }
447 
449  rcp(new SolutionState<Scalar>(metaData_nc, x_nc, xdot_nc, xdotdot_nc,
450  stepperState_nc, physicsState_nc));
451 
452  return ss;
453 }
454 
455 } // namespace Tempus
456 #endif // Tempus_SolutionState_impl_hpp
Teuchos::RCP< SolutionState< Scalar > > createSolutionStateX(const Teuchos::RCP< Thyra::VectorBase< Scalar > > &x, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &xdot=Teuchos::null, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &xdotdot=Teuchos::null)
Nonmember constructor from non-const solution vectors, x.
PhysicsState is a simple class to hold information about the physics.
Teuchos::RCP< const SolutionStateMetaData< Scalar > > metaData_
Meta Data for the solution state.
bool is_null(const std::shared_ptr< T > &p)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Teuchos::RCP< PhysicsState< Scalar > > physicsState_nc_
Teuchos::RCP< const PhysicsState< Scalar > > physicsState_
PhysicsState for this SolutionState.
StepperState is a simple class to hold state information about the stepper.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
bool operator>(const SolutionState< Scalar > &ss) const
Greater than comparison for sorting based on time.
Teuchos::RCP< const StepperState< Scalar > > stepperState_
StepperState for this SolutionState.
Ptr< T > ptr() const
virtual void copySolutionData(const Teuchos::RCP< const SolutionState< Scalar > > &s)
Deep copy solution data, but keep metaData untouched.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
SolutionState()
Default Constructor – Not meant for immediate adding to SolutionHistory. This constructor does not se...
bool operator>=(const SolutionState< Scalar > &ss) const
Greater than or equal to comparison for sorting based on time.
Teuchos::RCP< SolutionState< Scalar > > createSolutionStateME(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model, const Teuchos::RCP< StepperState< Scalar > > &stepperState=Teuchos::null, const Teuchos::RCP< PhysicsState< Scalar > > &physicsState=Teuchos::null)
Nonmember constructor from Thyra ModelEvaluator.
bool operator==(const SolutionState< Scalar > &ss) const
Equality comparison for matching.
Teuchos::RCP< StepperState< Scalar > > stepperState_nc_
RCP< std::basic_ostream< char_type, traits_type > > getOStream()
bool operator<=(const SolutionState< Scalar > &ss) const
Less than or equal to comparison for sorting based on time.
virtual Teuchos::RCP< SolutionState< Scalar > > clone() const
This is a deep copy constructor.
virtual void computeNorms(const Teuchos::RCP< const SolutionState< Scalar > > &ssIn=Teuchos::null)
Compute the solution norms, and solution change from ssIn, if provided.
bool operator<(const SolutionState< Scalar > &ss) const
Less than comparison for sorting based on time.
virtual std::string description() const
Solution state for integrators and steppers.
Teuchos::RCP< SolutionStateMetaData< Scalar > > metaData_nc_
virtual void copy(const Teuchos::RCP< const SolutionState< Scalar > > &ss)
This is a deep copy.