Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_StepperIMEX_RK_Partition_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_StepperIMEX_RK_Partition_impl_hpp
10 #define Tempus_StepperIMEX_RK_Partition_impl_hpp
11 
12 #include "Thyra_VectorStdOps.hpp"
13 
14 #include "Tempus_StepperFactory.hpp"
16 
17 namespace Tempus {
18 
19 template <class Scalar>
21  std::string stepperType)
22 {
24  stepperType != "Partitioned IMEX RK 1st order" &&
25  stepperType != "Partitioned IMEX RK SSP2" &&
26  stepperType != "Partitioned IMEX RK ARS 233" &&
27  stepperType != "General Partitioned IMEX RK",
28  std::logic_error,
29  " 'Stepper Type' (='"
30  << stepperType
31  << "')\n"
32  << " does not match one of the types for this Stepper:\n"
33  << " 'Partitioned IMEX RK 1st order'\n"
34  << " 'Partitioned IMEX RK SSP2'\n"
35  << " 'Partitioned IMEX RK ARS 233'\n"
36  << " 'General Partitioned IMEX RK'\n");
37 
38  this->setStepperName("Partitioned IMEX RK SSP2");
39  this->setStepperType("Partitioned IMEX RK SSP2");
40  this->setUseFSAL(false);
41  this->setICConsistency("None");
42  this->setICConsistencyCheck(false);
43  this->setZeroInitialGuess(false);
44 
45  this->setStageNumber(-1);
46 
47  this->setTableaus(stepperType);
48  this->setAppAction(Teuchos::null);
49  this->setDefaultSolver();
50 }
51 
52 template <class Scalar>
54  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel,
56  bool useFSAL, std::string ICConsistency, bool ICConsistencyCheck,
57  bool zeroInitialGuess,
58  const Teuchos::RCP<StepperRKAppAction<Scalar> >& stepperRKAppAction,
59  std::string stepperType,
60  Teuchos::RCP<const RKButcherTableau<Scalar> > explicitTableau,
61  Teuchos::RCP<const RKButcherTableau<Scalar> > implicitTableau, Scalar order)
62 {
64  stepperType != "Partitioned IMEX RK 1st order" &&
65  stepperType != "Partitioned IMEX RK SSP2" &&
66  stepperType != "Partitioned IMEX RK ARS 233" &&
67  stepperType != "General Partitioned IMEX RK",
68  std::logic_error,
69  " 'Stepper Type' (='"
70  << stepperType
71  << "')\n"
72  << " does not match one of the types for this Stepper:\n"
73  << " 'Partitioned IMEX RK 1st order'\n"
74  << " 'Partitioned IMEX RK SSP2'\n"
75  << " 'Partitioned IMEX RK ARS 233'\n"
76  << " 'General Partitioned IMEX RK'\n");
77 
78  this->setStepperName(stepperType);
79  this->setStepperType(stepperType);
80  this->setUseFSAL(useFSAL);
81  this->setICConsistency(ICConsistency);
82  this->setICConsistencyCheck(ICConsistencyCheck);
83  this->setZeroInitialGuess(zeroInitialGuess);
84  this->setOrder(order);
85 
86  this->setStageNumber(-1);
87 
88  if (stepperType == "General Partitioned IMEX RK") {
89  this->setExplicitTableau(explicitTableau);
90  this->setImplicitTableau(implicitTableau);
91  }
92  else {
93  this->setTableaus(stepperType);
94  }
95  this->setAppAction(stepperRKAppAction);
96  this->setSolver(solver);
97 
98  if (appModel != Teuchos::null) {
99  this->setModel(appModel);
100  this->initialize();
101  }
102 }
103 
104 template <class Scalar>
106  std::string stepperType,
107  Teuchos::RCP<const RKButcherTableau<Scalar> > explicitTableau,
108  Teuchos::RCP<const RKButcherTableau<Scalar> > implicitTableau)
109 {
110  if (stepperType == "") stepperType = "Partitioned IMEX RK SSP2";
111 
112  if (stepperType == "Partitioned IMEX RK 1st order") {
113  {
114  // Explicit Tableau
116  int NumStages = 2;
117  Teuchos::SerialDenseMatrix<int, Scalar> A(NumStages, NumStages);
120  const Scalar one = ST::one();
121  const Scalar zero = ST::zero();
122 
123  // Fill A:
124  A(0, 0) = zero;
125  A(0, 1) = zero;
126  A(1, 0) = one;
127  A(1, 1) = zero;
128 
129  // Fill b:
130  b(0) = one;
131  b(1) = zero;
132 
133  // Fill c:
134  c(0) = zero;
135  c(1) = one;
136 
137  int order = 1;
138 
139  auto expTableau = Teuchos::rcp(new RKButcherTableau<Scalar>(
140  "Explicit Tableau - Partitioned IMEX RK 1st order", A, b, c, order,
141  order, order));
142  expTableau->setTVD(true);
143  expTableau->setTVDCoeff(2.0);
144 
145  this->setExplicitTableau(expTableau);
146  }
147  {
148  // Implicit Tableau
150  int NumStages = 2;
151  const Scalar sspcoef = std::numeric_limits<Scalar>::max();
152  Teuchos::SerialDenseMatrix<int, Scalar> A(NumStages, NumStages);
155  const Scalar one = ST::one();
156  const Scalar zero = ST::zero();
157 
158  // Fill A:
159  A(0, 0) = zero;
160  A(0, 1) = zero;
161  A(1, 0) = zero;
162  A(1, 1) = one;
163 
164  // Fill b:
165  b(0) = zero;
166  b(1) = one;
167 
168  // Fill c:
169  c(0) = zero;
170  c(1) = one;
171 
172  int order = 1;
173 
174  auto impTableau = Teuchos::rcp(new RKButcherTableau<Scalar>(
175  "Implicit Tableau - Partitioned IMEX RK 1st order", A, b, c, order,
176  order, order));
177  impTableau->setTVD(true);
178  impTableau->setTVDCoeff(sspcoef);
179 
180  this->setImplicitTableau(impTableau);
181  }
182  this->setStepperName("Partitioned IMEX RK 1st order");
183  this->setStepperType("Partitioned IMEX RK 1st order");
184  this->setOrder(1);
185  }
186  else if (stepperType == "Partitioned IMEX RK SSP2") {
187  // Explicit Tableau
188  auto stepperERK = Teuchos::rcp(new StepperERK_Trapezoidal<Scalar>());
189  this->setExplicitTableau(stepperERK->getTableau());
190 
191  // Implicit Tableau
192  auto stepperSDIRK = Teuchos::rcp(new StepperSDIRK_2Stage3rdOrder<Scalar>());
193  stepperSDIRK->setGammaType("2nd Order L-stable");
194  this->setImplicitTableau(stepperSDIRK->getTableau());
195 
196  this->setStepperName("Partitioned IMEX RK SSP2");
197  this->setStepperType("Partitioned IMEX RK SSP2");
198  this->setOrder(2);
199  }
200  else if (stepperType == "Partitioned IMEX RK ARS 233") {
202  int NumStages = 3;
203  Teuchos::SerialDenseMatrix<int, Scalar> A(NumStages, NumStages);
206  const Scalar one = ST::one();
207  const Scalar zero = ST::zero();
208  const Scalar onehalf = ST::one() / (2 * ST::one());
209  const Scalar gamma = (3 * one + ST::squareroot(3 * one)) / (6 * one);
210  {
211  // Explicit Tableau
212  // Fill A:
213  A(0, 0) = zero;
214  A(0, 1) = zero;
215  A(0, 2) = zero;
216  A(1, 0) = gamma;
217  A(1, 1) = zero;
218  A(1, 2) = zero;
219  A(2, 0) = (gamma - 1.0);
220  A(2, 1) = (2.0 - 2.0 * gamma);
221  A(2, 2) = zero;
222 
223  // Fill b:
224  b(0) = zero;
225  b(1) = onehalf;
226  b(2) = onehalf;
227 
228  // Fill c:
229  c(0) = zero;
230  c(1) = gamma;
231  c(2) = one - gamma;
232 
233  int order = 2;
234 
235  auto expTableau = Teuchos::rcp(new RKButcherTableau<Scalar>(
236  "Explicit Tableau - Partitioned IMEX RK ARS 233", A, b, c, order,
237  order, order));
238 
239  this->setExplicitTableau(expTableau);
240  }
241  {
242  // Implicit Tableau
243  // Fill A:
244  A(0, 0) = zero;
245  A(0, 1) = zero;
246  A(0, 2) = zero;
247  A(1, 0) = zero;
248  A(1, 1) = gamma;
249  A(1, 2) = zero;
250  A(2, 0) = zero;
251  A(2, 1) = (1.0 - 2.0 * gamma);
252  A(2, 2) = gamma;
253 
254  // Fill b:
255  b(0) = zero;
256  b(1) = onehalf;
257  b(2) = onehalf;
258 
259  // Fill c:
260  c(0) = zero;
261  c(1) = gamma;
262  c(2) = one - gamma;
263 
264  int order = 3;
265 
266  auto impTableau = Teuchos::rcp(new RKButcherTableau<Scalar>(
267  "Implicit Tableau - Partitioned IMEX RK ARS 233", A, b, c, order,
268  order, order));
269 
270  this->setImplicitTableau(impTableau);
271  }
272  this->setStepperName("Partitioned IMEX RK ARS 233");
273  this->setStepperType("Partitioned IMEX RK ARS 233");
274  this->setOrder(3);
275  }
276  else if (stepperType == "General Partitioned IMEX RK") {
277  if (explicitTableau == Teuchos::null) {
278  // Default Explicit Tableau (i.e., Partitioned IMEX RK SSP2)
279  auto stepperERK = Teuchos::rcp(new StepperERK_Trapezoidal<Scalar>());
280  this->setExplicitTableau(stepperERK->getTableau());
281  }
282  else {
283  this->setExplicitTableau(explicitTableau);
284  }
285 
286  if (implicitTableau == Teuchos::null) {
287  // Default Implicit Tablea (i.e., Partitioned IMEX RK SSP2)
288  auto stepperSDIRK =
290  stepperSDIRK->setGammaType("2nd Order L-stable");
291  this->setImplicitTableau(stepperSDIRK->getTableau());
292  }
293  else {
294  this->setImplicitTableau(implicitTableau);
295  }
296 
297  this->setStepperName("General Partitioned IMEX RK");
298  this->setStepperType("General Partitioned IMEX RK");
299  this->setOrder(1);
300  }
301  else {
303  true, std::logic_error,
304  "Error - Not a valid StepperIMEX_RK_Partition type! Stepper Type = "
305  << stepperType << "\n"
306  << " Current valid types are: \n"
307  << " 'Partitioned IMEX RK 1st order'\n"
308  << " 'Partitioned IMEX RK SSP2'\n"
309  << " 'Partitioned IMEX RK ARS 233'\n"
310  << " 'General Partitioned IMEX RK'\n");
311  }
312 
314  explicitTableau_ == Teuchos::null, std::runtime_error,
315  "Error - StepperIMEX_RK_Partition - Explicit tableau is null!");
317  implicitTableau_ == Teuchos::null, std::runtime_error,
318  "Error - StepperIMEX_RK_Partition - Implicit tableau is null!");
320  explicitTableau_->numStages() != implicitTableau_->numStages(),
321  std::runtime_error,
322  "Error - StepperIMEX_RK_Partition - Number of stages do not match!\n"
323  << " Explicit tableau = " << explicitTableau_->description() << "\n"
324  << " number of stages = " << explicitTableau_->numStages() << "\n"
325  << " Implicit tableau = " << implicitTableau_->description() << "\n"
326  << " number of stages = " << implicitTableau_->numStages()
327  << "\n");
328 
329  this->isInitialized_ = false;
330 }
331 
332 template <class Scalar>
334  Teuchos::RCP<Teuchos::ParameterList> pl, std::string stepperType)
335 {
336  using Teuchos::RCP;
337  if (stepperType == "") {
338  if (pl == Teuchos::null)
339  stepperType = "Partitioned IMEX RK SSP2";
340  else
341  stepperType =
342  pl->get<std::string>("Stepper Type", "Partitioned IMEX RK SSP2");
343  }
344 
345  if (stepperType != "General Partitioned IMEX RK") {
346  this->setTableaus(stepperType);
347  }
348  else {
349  if (pl != Teuchos::null) {
352  if (pl->isSublist("IMEX-RK Explicit Stepper")) {
353  RCP<Teuchos::ParameterList> explicitPL =
355  pl->sublist("IMEX-RK Explicit Stepper")));
356  auto sf = Teuchos::rcp(new StepperFactory<Scalar>());
357  auto stepperTemp = sf->createStepper(explicitPL, Teuchos::null);
358  auto stepperERK = Teuchos::rcp_dynamic_cast<StepperExplicitRK<Scalar> >(
359  stepperTemp, true);
361  stepperERK == Teuchos::null, std::logic_error,
362  "Error - The explicit component of a general partitioned IMEX RK "
363  "stepper was not specified as an ExplicitRK stepper");
364  explicitTableau = stepperERK->getTableau();
365  }
366 
367  if (pl->isSublist("IMEX-RK Implicit Stepper")) {
368  RCP<Teuchos::ParameterList> implicitPL =
370  pl->sublist("IMEX-RK Implicit Stepper")));
371  auto sf = Teuchos::rcp(new StepperFactory<Scalar>());
372  auto stepperTemp = sf->createStepper(implicitPL, Teuchos::null);
373  auto stepperDIRK =
374  Teuchos::rcp_dynamic_cast<StepperDIRK<Scalar> >(stepperTemp, true);
376  stepperDIRK == Teuchos::null, std::logic_error,
377  "Error - The implicit component of a general partitioned IMEX RK "
378  "stepper was not specified as an DIRK stepper");
379  implicitTableau = stepperDIRK->getTableau();
380  }
381 
383  !(explicitTableau != Teuchos::null &&
384  implicitTableau != Teuchos::null),
385  std::logic_error,
386  "Error - A parameter list was used to setup a general partitioned "
387  "IMEX RK stepper, but did not "
388  "specify both an explicit and an implicit tableau!\n");
389 
390  this->setTableaus(stepperType, explicitTableau, implicitTableau);
391 
392  this->setOrder(pl->get<int>("overall order", 1));
393  }
394  }
395 }
396 
397 template <class Scalar>
399  Teuchos::RCP<const RKButcherTableau<Scalar> > explicitTableau)
400 {
402  explicitTableau->isImplicit() == true, std::logic_error,
403  "Error - Received an implicit Tableau for setExplicitTableau()!\n"
404  << " Tableau = " << explicitTableau->description() << "\n");
405  explicitTableau_ = explicitTableau;
406 
407  this->isInitialized_ = false;
408 }
409 
410 template <class Scalar>
412  Teuchos::RCP<const RKButcherTableau<Scalar> > implicitTableau)
413 {
415  implicitTableau->isDIRK() != true, std::logic_error,
416  "Error - Did not receive a DIRK Tableau for setImplicitTableau()!\n"
417  << " Tableau = " << implicitTableau->description() << "\n");
418  implicitTableau_ = implicitTableau;
419 
420  this->isInitialized_ = false;
421 }
422 
423 template <class Scalar>
425  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& appModel)
426 {
427  using Teuchos::RCP;
428  using Teuchos::rcp_const_cast;
429  using Teuchos::rcp_dynamic_cast;
430  RCP<Thyra::ModelEvaluator<Scalar> > ncModel =
431  rcp_const_cast<Thyra::ModelEvaluator<Scalar> >(appModel);
432  RCP<WrapperModelEvaluatorPairPartIMEX_Basic<Scalar> > modelPairIMEX =
434  ncModel);
436  modelPairIMEX == Teuchos::null, std::logic_error,
437  "Error - StepperIMEX_RK::setModel() was given a ModelEvaluator that\n"
438  << " could not be cast to a WrapperModelEvaluatorPairPartIMEX_Basic!\n"
439  << " From: " << appModel << "\n To : " << modelPairIMEX
440  << "\n Likely have given the wrong ModelEvaluator to this Stepper.\n");
441 
442  setModelPair(modelPairIMEX);
443 
444  this->isInitialized_ = false;
445 }
446 
452 template <class Scalar>
455  mePairIMEX)
456 {
458  wrapperModelPairPartIMEX = Teuchos::rcp_dynamic_cast<
460  this->wrapperModel_);
461  validExplicitODE(mePairIMEX->getExplicitModel());
462  validImplicitODE_DAE(mePairIMEX->getImplicitModel());
463  wrapperModelPairPartIMEX = mePairIMEX;
464  wrapperModelPairPartIMEX->initialize();
465 
466  this->wrapperModel_ = wrapperModelPairPartIMEX;
467 
468  this->isInitialized_ = false;
469 }
470 
476 template <class Scalar>
478  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& explicitModel,
479  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& implicitModel)
480 {
481  validExplicitODE(explicitModel);
482  validImplicitODE_DAE(implicitModel);
483  this->wrapperModel_ =
485  explicitModel, implicitModel));
486 
487  this->isInitialized_ = false;
488 }
489 
490 template <class Scalar>
492 {
494  wrapperModelPairPartIMEX = Teuchos::rcp_dynamic_cast<
496  this->wrapperModel_);
498  wrapperModelPairPartIMEX == Teuchos::null, std::logic_error,
499  "Error - Can not cast the wrapper Model Evaluator to a IMEX Model Pair."
500  "StepperIMEX_RK_Partition::initialize()\n");
501 
502  // Initialize the stage vectors
503  const int numStages = explicitTableau_->numStages();
504  stageF_.resize(numStages);
505  stageGx_.resize(numStages);
506  for (int i = 0; i < numStages; i++) {
507  stageF_[i] = Thyra::createMember(
508  wrapperModelPairPartIMEX->getExplicitModel()->get_f_space());
509  stageGx_[i] = Thyra::createMember(
510  wrapperModelPairPartIMEX->getImplicitModel()->get_f_space());
511  assign(stageF_[i].ptr(), Teuchos::ScalarTraits<Scalar>::zero());
512  assign(stageGx_[i].ptr(), Teuchos::ScalarTraits<Scalar>::zero());
513  }
514 
515  xTilde_ = Thyra::createMember(
516  wrapperModelPairPartIMEX->getImplicitModel()->get_x_space());
517  assign(xTilde_.ptr(), Teuchos::ScalarTraits<Scalar>::zero());
518 
520 }
521 
522 template <class Scalar>
524  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
525 {
526  using Teuchos::RCP;
527 
528  int numStates = solutionHistory->getNumStates();
529 
531  numStates < 1, std::logic_error,
532  "Error - setInitialConditions() needs at least one SolutionState\n"
533  " to set the initial condition. Number of States = "
534  << numStates);
535 
536  if (numStates > 1) {
537  RCP<Teuchos::FancyOStream> out = this->getOStream();
538  Teuchos::OSTab ostab(out, 1, "StepperIMEX_RK::setInitialConditions()");
539  *out << "Warning -- SolutionHistory has more than one state!\n"
540  << "Setting the initial conditions on the currentState.\n"
541  << std::endl;
542  }
543 
544  RCP<SolutionState<Scalar> > initialState = solutionHistory->getCurrentState();
545  RCP<Thyra::VectorBase<Scalar> > x = initialState->getX();
546 
547  // Use x from inArgs as ICs, if needed.
548  auto inArgs = this->wrapperModel_->getNominalValues();
549  if (x == Teuchos::null) {
551  (x == Teuchos::null) && (inArgs.get_x() == Teuchos::null),
552  std::logic_error,
553  "Error - setInitialConditions() needs the ICs from the "
554  "SolutionHistory\n or getNominalValues()!\n");
555 
556  x = Teuchos::rcp_const_cast<Thyra::VectorBase<Scalar> >(inArgs.get_x());
557  initialState->setX(x);
558  }
559 
560  // Perform IC Consistency
561  std::string icConsistency = this->getICConsistency();
563  icConsistency != "None", std::logic_error,
564  "Error - setInitialConditions() requested a consistency of '"
565  << icConsistency
566  << "'.\n But only 'None' is available for IMEX-RK!\n");
567 
569  this->getUseFSAL(), std::logic_error,
570  "Error - The First-Same-As-Last (FSAL) principle is not "
571  << "available for IMEX-RK. Set useFSAL=false.\n");
572 }
573 
574 template <typename Scalar>
576  const Teuchos::RCP<const Thyra::VectorBase<Scalar> >& X,
577  const Teuchos::RCP<const Thyra::VectorBase<Scalar> >& Y, Scalar time,
578  Scalar stepSize, Scalar stageNumber,
579  const Teuchos::RCP<Thyra::VectorBase<Scalar> >& G) const
580 {
581  typedef Thyra::ModelEvaluatorBase MEB;
583  wrapperModelPairPartIMEX = Teuchos::rcp_dynamic_cast<
585  this->wrapperModel_);
586  MEB::InArgs<Scalar> inArgs = wrapperModelPairPartIMEX->getInArgs();
587  inArgs.set_x(X);
588  inArgs.set_p(wrapperModelPairPartIMEX->getParameterIndex(), Y);
589  if (inArgs.supports(MEB::IN_ARG_t)) inArgs.set_t(time);
590  if (inArgs.supports(MEB::IN_ARG_step_size)) inArgs.set_step_size(stepSize);
591  if (inArgs.supports(MEB::IN_ARG_stage_number))
592  inArgs.set_stage_number(stageNumber);
593 
594  // For model evaluators whose state function f(x, x_dot, t) describes
595  // an implicit ODE, and which accept an optional x_dot input argument,
596  // make sure the latter is set to null in order to request the evaluation
597  // of a state function corresponding to the explicit ODE formulation
598  // x_dot = f(x, t)
599  if (inArgs.supports(MEB::IN_ARG_x_dot)) inArgs.set_x_dot(Teuchos::null);
600 
601  MEB::OutArgs<Scalar> outArgs = wrapperModelPairPartIMEX->getOutArgs();
602  outArgs.set_f(G);
603 
604  wrapperModelPairPartIMEX->getImplicitModel()->evalModel(inArgs, outArgs);
605  Thyra::Vt_S(G.ptr(), -1.0);
606 }
607 
608 template <typename Scalar>
610  const Teuchos::RCP<const Thyra::VectorBase<Scalar> >& Z, Scalar time,
611  Scalar stepSize, Scalar stageNumber,
612  const Teuchos::RCP<Thyra::VectorBase<Scalar> >& F) const
613 {
614  typedef Thyra::ModelEvaluatorBase MEB;
615 
617  wrapperModelPairPartIMEX = Teuchos::rcp_dynamic_cast<
619  this->wrapperModel_);
620  MEB::InArgs<Scalar> inArgs =
621  wrapperModelPairPartIMEX->getExplicitModel()->createInArgs();
622  inArgs.set_x(Z);
623  if (inArgs.supports(MEB::IN_ARG_t)) inArgs.set_t(time);
624  if (inArgs.supports(MEB::IN_ARG_step_size)) inArgs.set_step_size(stepSize);
625  if (inArgs.supports(MEB::IN_ARG_stage_number))
626  inArgs.set_stage_number(stageNumber);
627 
628  // For model evaluators whose state function f(x, x_dot, t) describes
629  // an implicit ODE, and which accept an optional x_dot input argument,
630  // make sure the latter is set to null in order to request the evaluation
631  // of a state function corresponding to the explicit ODE formulation
632  // x_dot = f(x, t)
633  if (inArgs.supports(MEB::IN_ARG_x_dot)) inArgs.set_x_dot(Teuchos::null);
634 
635  MEB::OutArgs<Scalar> outArgs =
636  wrapperModelPairPartIMEX->getExplicitModel()->createOutArgs();
637  outArgs.set_f(F);
638 
639  wrapperModelPairPartIMEX->getExplicitModel()->evalModel(inArgs, outArgs);
640  Thyra::Vt_S(F.ptr(), -1.0);
641 }
642 
643 template <class Scalar>
645  const Teuchos::RCP<SolutionHistory<Scalar> >& solutionHistory)
646 {
647  this->checkInitialized();
648 
649  using Teuchos::RCP;
652 
653  TEMPUS_FUNC_TIME_MONITOR("Tempus::StepperIMEX_RK_Partition::takeStep()");
654  {
656  solutionHistory->getNumStates() < 2, std::logic_error,
657  "Error - StepperIMEX_RK_Partition<Scalar>::takeStep(...)\n"
658  << "Need at least two SolutionStates for IMEX_RK_Partition.\n"
659  << " Number of States = " << solutionHistory->getNumStates()
660  << "\nTry setting in \"Solution History\" \"Storage Type\" = "
661  << "\"Undo\"\n or \"Storage Type\" = \"Static\" and \"Storage Limit\" = "
662  << "\"2\"\n");
663 
664  RCP<SolutionState<Scalar> > currentState =
665  solutionHistory->getCurrentState();
666  RCP<SolutionState<Scalar> > workingState =
667  solutionHistory->getWorkingState();
668  const Scalar dt = workingState->getTimeStep();
669  const Scalar time = currentState->getTime();
670 
671  const int numStages = explicitTableau_->numStages();
672  const SerialDenseMatrix<int, Scalar>& AHat = explicitTableau_->A();
673  const SerialDenseVector<int, Scalar>& bHat = explicitTableau_->b();
674  const SerialDenseVector<int, Scalar>& cHat = explicitTableau_->c();
675  const SerialDenseMatrix<int, Scalar>& A = implicitTableau_->A();
676  const SerialDenseVector<int, Scalar>& b = implicitTableau_->b();
677  const SerialDenseVector<int, Scalar>& c = implicitTableau_->c();
678 
680  wrapperModelPairPartIMEX = Teuchos::rcp_dynamic_cast<
682  this->wrapperModel_);
683 
684  bool pass = true;
685  Thyra::assign(workingState->getX().ptr(), *(currentState->getX()));
686  RCP<Thyra::VectorBase<Scalar> > stageY =
687  wrapperModelPairPartIMEX->getExplicitOnlyVector(workingState->getX());
688  RCP<Thyra::VectorBase<Scalar> > stageX =
689  wrapperModelPairPartIMEX->getIMEXVector(workingState->getX());
690 
691  RCP<StepperIMEX_RK_Partition<Scalar> > thisStepper =
692  Teuchos::rcpFromRef(*this);
693  this->stepperRKAppAction_->execute(
694  solutionHistory, thisStepper,
696 
697  // Compute stage solutions
698  for (int i = 0; i < numStages; ++i) {
699  this->setStageNumber(i);
700 
701  Thyra::assign(stageY.ptr(),
702  *(wrapperModelPairPartIMEX->getExplicitOnlyVector(
703  currentState->getX())));
704  Thyra::assign(
705  xTilde_.ptr(),
706  *(wrapperModelPairPartIMEX->getIMEXVector(currentState->getX())));
707  for (int j = 0; j < i; ++j) {
708  if (AHat(i, j) != Teuchos::ScalarTraits<Scalar>::zero()) {
709  RCP<Thyra::VectorBase<Scalar> > stageFy =
710  wrapperModelPairPartIMEX->getExplicitOnlyVector(stageF_[j]);
711  RCP<Thyra::VectorBase<Scalar> > stageFx =
712  wrapperModelPairPartIMEX->getIMEXVector(stageF_[j]);
713  Thyra::Vp_StV(stageY.ptr(), -dt * AHat(i, j), *stageFy);
714  Thyra::Vp_StV(xTilde_.ptr(), -dt * AHat(i, j), *stageFx);
715  }
716  if (A(i, j) != Teuchos::ScalarTraits<Scalar>::zero())
717  Thyra::Vp_StV(xTilde_.ptr(), -dt * A(i, j), *(stageGx_[j]));
718  }
719 
720  this->stepperRKAppAction_->execute(
721  solutionHistory, thisStepper,
723 
724  Scalar ts = time + c(i) * dt;
725  Scalar tHats = time + cHat(i) * dt;
726  if (A(i, i) == Teuchos::ScalarTraits<Scalar>::zero()) {
727  // Explicit stage for the ImplicitODE_DAE
728  bool isNeeded = false;
729  for (int k = i + 1; k < numStages; ++k)
730  if (A(k, i) != 0.0) isNeeded = true;
731  if (b(i) != 0.0) isNeeded = true;
732  if (isNeeded == false) {
733  // stageGx_[i] is not needed.
734  assign(stageGx_[i].ptr(), Teuchos::ScalarTraits<Scalar>::zero());
735  }
736  else {
737  Thyra::assign(stageX.ptr(), *xTilde_);
738  evalImplicitModelExplicitly(stageX, stageY, ts, dt, i, stageGx_[i]);
739  }
740  }
741  else {
742  // Implicit stage for the ImplicitODE_DAE
743  const Scalar alpha = Scalar(1.0) / (dt * A(i, i));
744  const Scalar beta = Scalar(1.0);
745 
746  // Setup TimeDerivative
747  RCP<TimeDerivative<Scalar> > timeDer =
749  alpha, xTilde_.getConst()));
750 
752  timeDer, dt, alpha, beta, SOLVE_FOR_X, i));
753 
754  this->stepperRKAppAction_->execute(
755  solutionHistory, thisStepper,
757 
758  wrapperModelPairPartIMEX->setUseImplicitModel(true);
759  this->solver_->setModel(wrapperModelPairPartIMEX);
760 
762  if (wrapperModelPairPartIMEX->getParameterIndex() >= 0)
763  sStatus = this->solveImplicitODE(
764  stageX, stageGx_[i], ts, p, stageY,
765  wrapperModelPairPartIMEX->getParameterIndex());
766  else
767  sStatus = this->solveImplicitODE(stageX, stageGx_[i], ts, p);
768 
769  if (sStatus.solveStatus != Thyra::SOLVE_STATUS_CONVERGED) pass = false;
770 
771  wrapperModelPairPartIMEX->setUseImplicitModel(false);
772 
773  this->stepperRKAppAction_->execute(
774  solutionHistory, thisStepper,
776 
777  // Update contributions to stage values
778  Thyra::V_StVpStV(stageGx_[i].ptr(), -alpha, *stageX, alpha, *xTilde_);
779  }
780 
781  this->stepperRKAppAction_->execute(
782  solutionHistory, thisStepper,
784  evalExplicitModel(workingState->getX(), tHats, dt, i, stageF_[i]);
785  this->stepperRKAppAction_->execute(
786  solutionHistory, thisStepper,
788  }
789 
790  // Sum for solution: y_n = y_n-1 - dt*Sum{ bHat(i)*fy(i) }
791  // Sum for solution: x_n = x_n-1 - dt*Sum{ bHat(i)*fx(i) + b(i)*gx(i) }
792  Thyra::assign((workingState->getX()).ptr(), *(currentState->getX()));
793  RCP<Thyra::VectorBase<Scalar> > Z = workingState->getX();
794  RCP<Thyra::VectorBase<Scalar> > X =
795  wrapperModelPairPartIMEX->getIMEXVector(Z);
796  for (int i = 0; i < numStages; ++i) {
797  if (bHat(i) != Teuchos::ScalarTraits<Scalar>::zero())
798  Thyra::Vp_StV(Z.ptr(), -dt * bHat(i), *(stageF_[i]));
800  Thyra::Vp_StV(X.ptr(), -dt * b(i), *(stageGx_[i]));
801  }
802 
803  if (pass == true)
804  workingState->setSolutionStatus(Status::PASSED);
805  else
806  workingState->setSolutionStatus(Status::FAILED);
807  workingState->setOrder(this->getOrder());
808  workingState->computeNorms(currentState);
809  this->stepperRKAppAction_->execute(
810  solutionHistory, thisStepper,
812  }
813  // reset the stage number
814  this->setStageNumber(-1);
815  return;
816 }
817 
824 template <class Scalar>
827 {
829  rcp(new StepperState<Scalar>(this->getStepperType()));
830  return stepperState;
831 }
832 
833 template <class Scalar>
835  Teuchos::FancyOStream& out, const Teuchos::EVerbosityLevel verbLevel) const
836 {
837  out.setOutputToRootOnly(0);
838  out << std::endl;
839  Stepper<Scalar>::describe(out, verbLevel);
840  StepperImplicit<Scalar>::describe(out, verbLevel);
841 
842  out << "--- StepperIMEX_RK_Partition ---\n";
843  out << " explicitTableau_ = " << explicitTableau_ << std::endl;
844  if (verbLevel == Teuchos::VERB_HIGH)
845  explicitTableau_->describe(out, verbLevel);
846  out << " implicitTableau_ = " << implicitTableau_ << std::endl;
847  if (verbLevel == Teuchos::VERB_HIGH)
848  implicitTableau_->describe(out, verbLevel);
849  out << " xTilde_ = " << xTilde_ << std::endl;
850  out << " stageF_.size() = " << stageF_.size() << std::endl;
851  int numStages = stageF_.size();
852  for (int i = 0; i < numStages; ++i)
853  out << " stageF_[" << i << "] = " << stageF_[i] << std::endl;
854  out << " stageGx_.size() = " << stageGx_.size() << std::endl;
855  numStages = stageGx_.size();
856  for (int i = 0; i < numStages; ++i)
857  out << " stageGx_[" << i << "] = " << stageGx_[i] << std::endl;
858  out << " stepperRKAppAction_= " << this->stepperRKAppAction_ << std::endl;
859  out << " order_ = " << order_ << std::endl;
860  out << "--------------------------------" << std::endl;
861 }
862 
863 template <class Scalar>
865  Teuchos::FancyOStream& out) const
866 {
867  out.setOutputToRootOnly(0);
868 
869  bool isValidSetup = true;
870 
871  if (!Stepper<Scalar>::isValidSetup(out)) isValidSetup = false;
872 
874  Teuchos::rcp_dynamic_cast<WrapperModelEvaluatorPairIMEX<Scalar> >(
875  this->wrapperModel_);
876 
877  if (wrapperModelPairIMEX->getExplicitModel() == Teuchos::null) {
878  isValidSetup = false;
879  out << "The explicit ModelEvaluator is not set!\n";
880  }
881 
882  if (wrapperModelPairIMEX->getImplicitModel() == Teuchos::null) {
883  isValidSetup = false;
884  out << "The implicit ModelEvaluator is not set!\n";
885  }
886 
887  if (this->wrapperModel_ == Teuchos::null) {
888  isValidSetup = false;
889  out << "The wrapper ModelEvaluator is not set!\n";
890  }
891 
892  if (this->solver_ == Teuchos::null) {
893  isValidSetup = false;
894  out << "The solver is not set!\n";
895  }
896 
897  if (this->stepperRKAppAction_ == Teuchos::null) {
898  isValidSetup = false;
899  out << "The AppAction is not set!\n";
900  }
901 
902  if (explicitTableau_ == Teuchos::null) {
903  isValidSetup = false;
904  out << "The explicit tableau is not set!\n";
905  }
906 
907  if (implicitTableau_ == Teuchos::null) {
908  isValidSetup = false;
909  out << "The implicit tableau is not set!\n";
910  }
911 
912  return isValidSetup;
913 }
914 
915 template <class Scalar>
918 {
919  auto pl = this->getValidParametersBasicImplicit();
920  pl->template set<int>("overall order", this->getOrder());
921 
922  auto explicitStepper = Teuchos::rcp(new StepperERK_General<Scalar>());
923  explicitStepper->setTableau(
924  explicitTableau_->A(), explicitTableau_->b(), explicitTableau_->c(),
925  explicitTableau_->order(), explicitTableau_->orderMin(),
926  explicitTableau_->orderMax(), explicitTableau_->bstar());
927  pl->set("IMEX-RK Explicit Stepper", *explicitStepper->getValidParameters());
928 
929  auto implicitStepper = Teuchos::rcp(new StepperERK_General<Scalar>());
930  implicitStepper->setTableau(
931  implicitTableau_->A(), implicitTableau_->b(), implicitTableau_->c(),
932  implicitTableau_->order(), implicitTableau_->orderMin(),
933  implicitTableau_->orderMax(), implicitTableau_->bstar());
934  pl->set("IMEX-RK Implicit Stepper", *implicitStepper->getValidParameters());
935 
936  return pl;
937 }
938 
939 // Nonmember constructor - ModelEvaluator and ParameterList
940 // ------------------------------------------------------------------------
941 template <class Scalar>
943  const Teuchos::RCP<const Thyra::ModelEvaluator<Scalar> >& model,
944  std::string stepperType, Teuchos::RCP<Teuchos::ParameterList> pl)
945 {
946  auto stepper =
948  stepper->setStepperImplicitValues(pl);
949  stepper->setTableausPartition(pl, stepperType);
950 
951  if (model != Teuchos::null) {
952  stepper->setModel(model);
953  stepper->initialize();
954  }
955 
956  return stepper;
957 }
958 
959 } // namespace Tempus
960 #endif // Tempus_StepperIMEX_RK_Partition_impl_hpp
Explicit Runge-Kutta time stepper.
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
StepperIMEX_RK_Partition(std::string stepperType="Partitioned IMEX RK SSP2")
Default constructor.
T & get(const std::string &name, T def_value)
General Explicit Runge-Kutta Butcher Tableau.
virtual void setTableaus(std::string stepperType="", Teuchos::RCP< const RKButcherTableau< Scalar > > explicitTableau=Teuchos::null, Teuchos::RCP< const RKButcherTableau< Scalar > > implicitTableau=Teuchos::null)
Set both the explicit and implicit tableau from ParameterList.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
virtual void setModelPair(const Teuchos::RCP< WrapperModelEvaluatorPairPartIMEX_Basic< Scalar > > &modelPair)
Create WrapperModelPairIMEX from user-supplied ModelEvaluator pair.
ModelEvaluator pair for implicit and explicit (IMEX) evaluations.
Partitioned Implicit-Explicit Runge-Kutta (IMEX-RK) time stepper.
void validExplicitODE(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model)
Validate that the model supports explicit ODE evaluation, f(x,t) [=xdot].
virtual void initialize()
Initialize after construction and changing input parameters.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const override
void evalExplicitModel(const Teuchos::RCP< const Thyra::VectorBase< Scalar > > &X, Scalar time, Scalar stepSize, Scalar stageNumber, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &F) const
Thyra Base interface for time steppers.
Diagonally Implicit Runge-Kutta (DIRK) time stepper.
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)
virtual Teuchos::RCP< Tempus::StepperState< Scalar > > getDefaultStepperState()
Provide a StepperState to the SolutionState. This Stepper does not have any special state data...
ModelEvaluator pair for implicit and explicit (IMEX) evaulations.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
Application Action for StepperRKBase.
bool isSublist(const std::string &name) const
void evalImplicitModelExplicitly(const Teuchos::RCP< const Thyra::VectorBase< Scalar > > &X, const Teuchos::RCP< const Thyra::VectorBase< Scalar > > &Y, Scalar time, Scalar stepSize, Scalar stageNumber, const Teuchos::RCP< Thyra::VectorBase< Scalar > > &G) const
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
basic_FancyOStream & setOutputToRootOnly(const int rootRank)
virtual void setExplicitTableau(Teuchos::RCP< const RKButcherTableau< Scalar > > explicitTableau)
Set the explicit tableau from tableau.
virtual bool isValidSetup(Teuchos::FancyOStream &out) const
ESolveStatus solveStatus
virtual void setImplicitTableau(Teuchos::RCP< const RKButcherTableau< Scalar > > implicitTableau)
Set the implicit tableau from tableau.
Time-derivative interface for Partitioned IMEX RK.
virtual void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
virtual void setModel(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &appModel)
Set the model.
Teuchos::RCP< StepperIMEX_RK_Partition< Scalar > > createStepperIMEX_RK_Partition(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model, std::string stepperType, Teuchos::RCP< Teuchos::ParameterList > pl)
Nonmember constructor - ModelEvaluator and ParameterList.
virtual void setTableausPartition(Teuchos::RCP< Teuchos::ParameterList > pl, std::string stepperType)
virtual void takeStep(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Take the specified timestep, dt, and return true if successful.
virtual void setInitialConditions(const Teuchos::RCP< SolutionHistory< Scalar > > &solutionHistory)
Set the initial conditions and make them consistent.
void validImplicitODE_DAE(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model)
Validate ME supports implicit ODE/DAE evaluation, f(xdot,x,t) [= 0].
virtual void initialize()
Initialize during construction and after changing input parameters.
Solve for x and determine xDot from x.