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: Time Integration and Sensitivity Analysis Package
4 //
5 // Copyright 2017 NTESS and the Tempus contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 //@HEADER
9 
10 #ifndef Tempus_SolutionState_impl_hpp
11 #define Tempus_SolutionState_impl_hpp
12 
13 #include "Thyra_VectorStdOps.hpp"
14 
15 namespace Tempus {
16 
17 template <class Scalar>
19  : x_(Teuchos::null),
20  x_nc_(Teuchos::null),
21  xdot_(Teuchos::null),
22  xdot_nc_(Teuchos::null),
23  xdotdot_(Teuchos::null),
24  xdotdot_nc_(Teuchos::null),
25  stepperState_(Teuchos::null),
26  stepperState_nc_(Teuchos::null),
27  physicsState_(Teuchos::null),
28  physicsState_nc_(Teuchos::null)
29 {
36 }
37 
38 template <class Scalar>
43  const Teuchos::RCP<Thyra::VectorBase<Scalar> >& xdotdot,
44  const Teuchos::RCP<StepperState<Scalar> >& stepperState,
45  const Teuchos::RCP<PhysicsState<Scalar> >& physicsState)
46  : metaData_(metaData),
47  metaData_nc_(metaData),
48  x_(x),
49  x_nc_(x),
50  xdot_(xdot),
51  xdot_nc_(xdot),
52  xdotdot_(xdotdot),
53  xdotdot_nc_(xdotdot),
54  stepperState_(stepperState),
55  stepperState_nc_(stepperState),
56  physicsState_(physicsState),
57  physicsState_nc_(physicsState)
58 {
59  if (stepperState_nc_ == Teuchos::null) {
62  }
63  if (physicsState_nc_ == Teuchos::null) {
66  }
67 }
68 
69 template <class Scalar>
71  const Teuchos::RCP<const SolutionStateMetaData<Scalar> > metaData,
72  const Teuchos::RCP<const Thyra::VectorBase<Scalar> >& x,
73  const Teuchos::RCP<const Thyra::VectorBase<Scalar> >& xdot,
74  const Teuchos::RCP<const Thyra::VectorBase<Scalar> >& xdotdot,
75  const Teuchos::RCP<const StepperState<Scalar> >& stepperState,
76  const Teuchos::RCP<const PhysicsState<Scalar> >& physicsState)
77  : metaData_(metaData),
78  metaData_nc_(Teuchos::null),
79  x_(x),
80  x_nc_(Teuchos::null),
81  xdot_(xdot),
82  xdot_nc_(Teuchos::null),
83  xdotdot_(xdotdot),
84  xdotdot_nc_(Teuchos::null),
85  stepperState_(stepperState),
86  stepperState_nc_(Teuchos::null),
87  physicsState_(physicsState),
88  physicsState_nc_(Teuchos::null)
89 {
90  if (stepperState_ == Teuchos::null) {
92  }
93  if (physicsState_ == Teuchos::null) {
95  }
96 }
97 
98 template <class Scalar>
100  : metaData_(ss_.metaData_),
101  metaData_nc_(ss_.metaData_nc_),
102  x_(ss_.x_),
103  x_nc_(ss_.x_nc_),
104  xdot_(ss_.xdot_),
105  xdot_nc_(ss_.xdot_nc_),
106  xdotdot_(ss_.xdotdot_),
107  xdotdot_nc_(ss_.xdotdot_nc_),
108  stepperState_(ss_.stepperState_),
109  stepperState_nc_(ss_.stepperState_nc_),
110  physicsState_(ss_.physicsState_),
111  physicsState_nc_(ss_.physicsState_nc_)
112 {
113 }
114 
115 template <class Scalar>
117 {
118  using Teuchos::RCP;
119 
120  RCP<SolutionStateMetaData<Scalar> > metaData_out;
121  if (!Teuchos::is_null(metaData_)) metaData_out = metaData_->clone();
122 
123  RCP<Thyra::VectorBase<Scalar> > x_out;
124  if (!Teuchos::is_null(x_)) x_out = x_->clone_v();
125 
126  RCP<Thyra::VectorBase<Scalar> > xdot_out;
127  if (!Teuchos::is_null(xdot_)) xdot_out = xdot_->clone_v();
128 
129  RCP<Thyra::VectorBase<Scalar> > xdotdot_out;
130  if (!Teuchos::is_null(xdotdot_)) xdotdot_out = xdotdot_->clone_v();
131 
132  RCP<StepperState<Scalar> > sS_out;
133  if (!Teuchos::is_null(stepperState_)) sS_out = stepperState_->clone();
134 
135  RCP<PhysicsState<Scalar> > pS_out;
136  if (!Teuchos::is_null(physicsState_)) pS_out = physicsState_->clone();
137 
138  RCP<SolutionState<Scalar> > ss_out = Teuchos::rcp(new SolutionState<Scalar>(
139  metaData_out, x_out, xdot_out, xdotdot_out, sS_out, pS_out));
140 
141  return ss_out;
142 }
143 
144 template <class Scalar>
146  const Teuchos::RCP<const SolutionState<Scalar> >& ss)
147 {
148  metaData_nc_->copy(ss->metaData_);
149  this->copySolutionData(ss);
150 }
151 
152 template <class Scalar>
154  const Teuchos::RCP<const SolutionState<Scalar> >& ss)
155 {
156  if (ss->x_ == Teuchos::null)
157  x_nc_ = Teuchos::null;
158  else {
159  if (x_nc_ == Teuchos::null) {
160  x_nc_ = ss->x_->clone_v();
161  }
162  else
163  Thyra::V_V(x_nc_.ptr(), *(ss->x_));
164  }
165  x_ = x_nc_;
166 
167  if (ss->xdot_ == Teuchos::null)
168  xdot_nc_ = Teuchos::null;
169  else {
170  if (xdot_nc_ == Teuchos::null)
171  xdot_nc_ = ss->xdot_->clone_v();
172  else
173  Thyra::V_V(xdot_nc_.ptr(), *(ss->xdot_));
174  }
175  xdot_ = xdot_nc_;
176 
177  if (ss->xdotdot_ == Teuchos::null)
178  xdotdot_nc_ = Teuchos::null;
179  else {
180  if (xdotdot_nc_ == Teuchos::null)
181  xdotdot_nc_ = ss->xdotdot_->clone_v();
182  else
183  Thyra::V_V(xdotdot_nc_.ptr(), *(ss->xdotdot_));
184  }
185  xdotdot_ = xdotdot_nc_;
186 
187  if (ss->stepperState_ == Teuchos::null)
188  stepperState_nc_ = Teuchos::null;
189  else {
190  if (stepperState_nc_ == Teuchos::null)
191  stepperState_nc_ = ss->stepperState_->clone();
192  else
193  stepperState_nc_->copy(ss->stepperState_);
194  }
195  stepperState_ = stepperState_nc_;
196 
197  if (ss->physicsState_ == Teuchos::null)
198  physicsState_nc_ = Teuchos::null;
199  else {
200  if (physicsState_nc_ == Teuchos::null)
201  physicsState_nc_ = ss->physicsState_->clone();
202  else
203  physicsState_nc_->copy(ss->physicsState_);
204  }
205  physicsState_ = physicsState_nc_;
206 }
207 
208 template <class Scalar>
210 {
211  return (this->metaData_->getTime() < ss.metaData_->getTime());
212 }
213 
214 template <class Scalar>
216 {
217  return (this->metaData_->getTime() <= ss.metaData_->getTime());
218 }
219 
220 template <class Scalar>
221 bool SolutionState<Scalar>::operator<(const Scalar& t) const
222 {
223  return (this->metaData_->getTime() < t);
224 }
225 
226 template <class Scalar>
227 bool SolutionState<Scalar>::operator<=(const Scalar& t) const
228 {
229  return (this->metaData_->getTime() <= t);
230 }
231 
232 template <class Scalar>
234 {
235  return (this->metaData_->getTime() > ss.metaData_->getTime());
236 }
237 
238 template <class Scalar>
240 {
241  return (this->metaData_->getTime() >= ss.metaData_->getTime());
242 }
243 
244 template <class Scalar>
245 bool SolutionState<Scalar>::operator>(const Scalar& t) const
246 {
247  return (this->metaData_->getTime() > t);
248 }
249 
250 template <class Scalar>
251 bool SolutionState<Scalar>::operator>=(const Scalar& t) const
252 {
253  return (this->metaData_->getTime() >= t);
254 }
255 
256 template <class Scalar>
258 {
259  return (this->metaData_->getTime() == ss.metaData_->getTime());
260 }
261 
262 template <class Scalar>
263 bool SolutionState<Scalar>::operator==(const Scalar& t) const
264 {
265  return (this->metaData_->getTime() == t);
266 }
267 
268 template <class Scalar>
270 {
271  std::ostringstream out;
272  out << "SolutionState"
273  << " (index =" << std::setw(6) << this->getIndex()
274  << "; time =" << std::setw(10) << std::setprecision(3) << this->getTime()
275  << "; dt =" << std::setw(10) << std::setprecision(3)
276  << this->getTimeStep() << ")";
277  return out.str();
278 }
279 
280 template <class Scalar>
282  Teuchos::FancyOStream& out, const Teuchos::EVerbosityLevel verbLevel) const
283 {
284  auto l_out = Teuchos::fancyOStream(out.getOStream());
285  Teuchos::OSTab ostab(*l_out, 2, this->description());
286  l_out->setOutputToRootOnly(0);
287 
288  *l_out << "\n--- " << this->description() << " ---" << std::endl;
289 
290  if (Teuchos::as<int>(verbLevel) >= Teuchos::as<int>(Teuchos::VERB_EXTREME)) {
291  metaData_->describe(*l_out, verbLevel);
292  *l_out << " x = " << std::endl;
293  x_->describe(*l_out, verbLevel);
294 
295  if (xdot_ != Teuchos::null) {
296  *l_out << " xdot_ = " << std::endl;
297  xdot_->describe(*l_out, verbLevel);
298  }
299  if (xdotdot_ != Teuchos::null) {
300  *l_out << " xdotdot = " << std::endl;
301  xdotdot_->describe(*l_out, verbLevel);
302  }
303 
304  if (stepperState_ != Teuchos::null)
305  stepperState_->describe(*l_out, verbLevel);
306  if (physicsState_ != Teuchos::null)
307  physicsState_->describe(*l_out, verbLevel);
308 
309  *l_out << std::string(this->description().length() + 8, '-') << std::endl;
310  }
311 }
312 
313 template <class Scalar>
315  const Teuchos::RCP<const SolutionState<Scalar> >& ssIn)
316 {
317  if (!getComputeNorms()) return;
318 
319  auto x = this->getX();
320  this->setXNormL2(Thyra::norm(*x));
321 
322  if (ssIn != Teuchos::null) {
323  auto xIn = ssIn->getX();
324 
325  // dx = x - xIn
327  Thyra::createMember(x->space());
328  Thyra::V_VmV(dx.ptr(), *x, *xIn);
329  Scalar dxNorm = Thyra::norm(*dx);
330  Scalar xInNorm = Thyra::norm(*xIn);
331  this->setDxNormL2Abs(dxNorm);
332  // Compute change, e.g., ||x^n-x^(n-1)||/||x^(n-1)||
333  const Scalar eps = std::numeric_limits<Scalar>::epsilon();
334  const Scalar min = std::numeric_limits<Scalar>::min();
335  if (xInNorm < min / eps) { // numerically zero
336  this->setDxNormL2Rel(std::numeric_limits<Scalar>::infinity());
337  }
338  else {
339  // this->setDxNormL2Rel(dxNorm/(xInNorm + eps));
340  this->setDxNormL2Rel(dxNorm / (xInNorm * (1.0 + 1.0e4 * eps)));
341  }
342  }
343 }
344 
345 // Nonmember constructors.
346 // ------------------------------------------------------------------------
347 
348 template <class Scalar>
352  const Teuchos::RCP<Thyra::VectorBase<Scalar> >& xdotdot)
353 {
356 
357  Teuchos::RCP<StepperState<Scalar> > stepperState_nc =
358  Teuchos::rcp(new StepperState<Scalar>("Default"));
359 
360  Teuchos::RCP<PhysicsState<Scalar> > physicsState_nc =
362 
364  metaData_nc, x, xdot, xdotdot, stepperState_nc, physicsState_nc));
365 
366  return ss;
367 }
368 
369 template <class Scalar>
371  const Teuchos::RCP<const Thyra::VectorBase<Scalar> >& x,
372  const Teuchos::RCP<const Thyra::VectorBase<Scalar> >& xdot,
373  const Teuchos::RCP<const Thyra::VectorBase<Scalar> >& xdotdot)
374 {
377 
379  Teuchos::rcp(new StepperState<Scalar>("Default"));
380 
383 
385  metaData, x, xdot, xdotdot, stepperState, physicsState));
386 
387  return ss;
388 }
389 
390 template <class Scalar>
392  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& model,
393  const Teuchos::RCP<StepperState<Scalar> >& stepperState,
394  const Teuchos::RCP<PhysicsState<Scalar> >& physicsState)
395 {
396  typedef Thyra::ModelEvaluatorBase MEB;
397  using Teuchos::rcp_const_cast;
398 
399  auto metaData_nc = Teuchos::rcp(new SolutionStateMetaData<Scalar>());
400  metaData_nc->setSolutionStatus(Status::PASSED);
401 
402  MEB::InArgs<Scalar> inArgs = model->getNominalValues();
403 
405  inArgs.supports(MEB::IN_ARG_x) == false, std::logic_error,
406  model->description() << "does not support an x solution vector!");
407 
408  // The solution vector, x, is required (usually).
409  auto x_nc = rcp_const_cast<Thyra::VectorBase<Scalar> >(inArgs.get_x());
410 
411  // The solution derivative, xdot, can be optional provided, based on
412  // application needs. Here we will base it on "supports" IN_ARG_x_dot.
413  // Depending on the stepper used, a temporary xdot vector may be created
414  // within the Stepper, but not moved to the SolutionState.
416  if (inArgs.supports(MEB::IN_ARG_x_dot)) {
417  xdot_nc = rcp_const_cast<Thyra::VectorBase<Scalar> >(inArgs.get_x_dot());
418  }
419  else {
420  xdot_nc = Teuchos::null;
421  }
422 
423  // Similar as xdot.
425  if (inArgs.supports(MEB::IN_ARG_x_dot_dot)) {
426  xdotdot_nc =
427  rcp_const_cast<Thyra::VectorBase<Scalar> >(inArgs.get_x_dot_dot());
428  }
429  else {
430  xdotdot_nc = Teuchos::null;
431  }
432 
433  Teuchos::RCP<StepperState<Scalar> > stepperState_nc;
434  if (stepperState == Teuchos::null) {
435  stepperState_nc = Teuchos::rcp(new StepperState<Scalar>()); // Use default
436  }
437  else {
438  stepperState_nc = stepperState;
439  }
440 
441  Teuchos::RCP<PhysicsState<Scalar> > physicsState_nc;
442  if (physicsState == Teuchos::null) {
443  physicsState_nc = Teuchos::rcp(new PhysicsState<Scalar>()); // Use default
444  }
445  else {
446  physicsState_nc = physicsState;
447  }
448 
450  rcp(new SolutionState<Scalar>(metaData_nc, x_nc, xdot_nc, xdotdot_nc,
451  stepperState_nc, physicsState_nc));
452 
453  return ss;
454 }
455 
456 } // namespace Tempus
457 #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.