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