Tempus  Version of the Day
Time Integration
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Tempus_UnitTest_SDIRK_21Pair.cpp
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 
10 
12 
13 #include "../TestModels/DahlquistTestModel.hpp"
14 
15 namespace Tempus_Unit_Test {
16 
18 using Teuchos::RCP;
19 using Teuchos::rcp;
20 using Teuchos::rcp_const_cast;
21 using Teuchos::rcp_dynamic_cast;
22 using Teuchos::sublist;
23 
24 // ************************************************************
25 // ************************************************************
26 TEUCHOS_UNIT_TEST(SDIRK_21Pair, Default_Construction)
27 {
28  auto stepper = rcp(new Tempus::StepperSDIRK_21Pair<double>());
30 
31  // Test stepper properties.
32  TEUCHOS_ASSERT(stepper->getOrder() == 2);
33 }
34 
35 // ************************************************************
36 // ************************************************************
37 TEUCHOS_UNIT_TEST(SDIRK_21Pair, StepperFactory_Construction)
38 {
39  auto model = rcp(new Tempus_Test::SinCosModel<double>());
40  testFactoryConstruction("SDIRK 2(1) Pair", model);
41 }
42 
43 // ************************************************************
44 // ************************************************************
45 TEUCHOS_UNIT_TEST(SDIRK_21Pair, AppAction)
46 {
47  auto stepper = rcp(new Tempus::StepperSDIRK_21Pair<double>());
48  auto model = rcp(new Tempus_Test::SinCosModel<double>());
49  testRKAppAction(stepper, model, out, success);
50 }
51 
52 // ************************************************************
53 // ************************************************************
54 class StepperRKModifierSDIRK21Test
55  : virtual public Tempus::StepperRKModifierBase<double> {
56  public:
58  StepperRKModifierSDIRK21Test(Teuchos::FancyOStream &Out, bool &Success)
59  : out(Out), success(Success)
60  {
61  }
62 
64  virtual ~StepperRKModifierSDIRK21Test() {}
65 
67  virtual void modify(
71  {
72  const double relTol = 1.0e-14;
73  auto stageNumber = stepper->getStageNumber();
74  Teuchos::SerialDenseVector<int, double> c = stepper->getTableau()->c();
75 
76  auto currentState = sh->getCurrentState();
77  auto workingState = sh->getWorkingState();
78  const double dt = workingState->getTimeStep();
79  double time = currentState->getTime();
80  if (stageNumber >= 0) time += c(stageNumber) * dt;
81 
82  auto x = workingState->getX();
83  auto xDot = workingState->getXDot();
84  if (xDot == Teuchos::null) xDot = stepper->getStepperXDot();
85 
86  switch (actLoc) {
88  {
89  auto DME = Teuchos::rcp_dynamic_cast<
91  stepper->getModel());
92  TEST_FLOATING_EQUALITY(DME->getLambda(), -1.0, relTol);
93  }
94  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
95 
96  const double x_0 = get_ele(*(x), 0);
97  const double xDot_0 = get_ele(*(xDot), 0);
98  TEST_FLOATING_EQUALITY(x_0, 1.0, relTol); // Should be x_0
99  TEST_FLOATING_EQUALITY(xDot_0, -1.0, relTol); // Should be xDot_0
100  TEST_ASSERT(std::abs(time) < relTol);
101  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
102  TEST_COMPARE(stageNumber, ==, -1);
103  break;
104  }
107  const double X_i = get_ele(*(x), 0);
108  const double f_i = get_ele(*(xDot), 0);
109 
110  if (stageNumber == 0) {
111  TEST_FLOATING_EQUALITY(X_i, 1.0, relTol);
112  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
113  TEST_ASSERT(std::abs(f_i) < relTol);
114  }
115  else if (stageNumber == 1) {
116  TEST_FLOATING_EQUALITY(X_i, 0.5, relTol);
117  TEST_FLOATING_EQUALITY(f_i, -1.0, relTol);
118  TEST_ASSERT(std::abs(time) < relTol);
119  }
120  else {
121  TEUCHOS_TEST_FOR_EXCEPT(!(-1 < stageNumber && stageNumber < 2));
122  }
123 
124  break;
125  }
129  const double X_i = get_ele(*(x), 0); // 0.5
130  const double f_i = get_ele(*(xDot), 0); // -0.5
131 
132  if (stageNumber == 0) {
133  // X_i = 1, f_1 = 0
134  TEST_FLOATING_EQUALITY(X_i, 0.5, relTol);
135  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
136  TEST_FLOATING_EQUALITY(f_i, -0.5, relTol);
137  }
138  else if (stageNumber == 1) {
139  // X_i = , f_i =
140  TEST_FLOATING_EQUALITY(X_i, 0.75, relTol);
141  TEST_FLOATING_EQUALITY(f_i, -0.75, relTol);
142  TEST_ASSERT(std::abs(time) < relTol);
143  }
144  else {
145  TEUCHOS_TEST_FOR_EXCEPT(!(-1 < stageNumber && stageNumber < 2));
146  }
147 
148  break;
149  }
151  const double x_1 = get_ele(*(x), 0);
152  time = workingState->getTime();
153  TEST_FLOATING_EQUALITY(x_1, 3.0 / 8.0, relTol); // Should be x_1
154  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
155  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
156  TEST_COMPARE(stageNumber, ==, -1);
157 
158  if (stepper->getUseEmbedded() == true) {
159  TEST_FLOATING_EQUALITY(workingState->getTolRel(), 1.0, relTol);
160  TEST_ASSERT(std::abs(workingState->getTolAbs()) < relTol);
161  // e = 0 from doxygen above.
162  TEST_ASSERT(std::abs(workingState->getErrorRel()) < relTol);
163  }
164  }
165  }
166  }
167 
168  private:
170  bool &success;
171 };
172 
173 // ************************************************************
174 // ************************************************************
175 TEUCHOS_UNIT_TEST(SDIRK_21Pair, Modifier)
176 {
177  auto stepper = rcp(new Tempus::StepperSDIRK_21Pair<double>());
180 
181  auto modifier = rcp(new StepperRKModifierSDIRK21Test(out, success));
182 
183  stepper->setModel(model);
184  stepper->setAppAction(modifier);
185  stepper->setICConsistency("Consistent");
186  stepper->setUseFSAL(false);
187  stepper->initialize();
188 
189  // Create a SolutionHistory.
190  auto solutionHistory = Tempus::createSolutionHistoryME(model);
191 
192  // Take one time step.
193  stepper->setInitialConditions(solutionHistory);
194  solutionHistory->initWorkingState();
195  double dt = 1.0;
196  solutionHistory->getWorkingState()->setTimeStep(dt);
197  solutionHistory->getWorkingState()->setTime(dt);
198  stepper->takeStep(solutionHistory); // Primary testing occurs in
199  // modifierX during takeStep().
200  // Test stepper properties.
201  TEUCHOS_ASSERT(stepper->getOrder() == 2);
202 }
203 
204 } // namespace Tempus_Unit_Test
void testDIRKAccessorsFullConstruction(const RCP< Tempus::StepperDIRK< double >> &stepper)
Unit test utility for ExplicitRK Stepper construction and accessors.
void testFactoryConstruction(std::string stepperType, const Teuchos::RCP< const Thyra::ModelEvaluator< double > > &model)
Unit test utility for Stepper construction through StepperFactory.
The classic Dahlquist Test Problem.
#define TEST_COMPARE(v1, comp, v2)
#define TEST_FLOATING_EQUALITY(v1, v2, tol)
#define TEST_ASSERT(v1)
Sine-Cosine model problem from Rythmos. This is a canonical Sine-Cosine differential equation with a...
Teuchos::RCP< SolutionHistory< Scalar > > createSolutionHistoryME(const Teuchos::RCP< const Thyra::ModelEvaluator< Scalar > > &model)
Nonmember contructor from a Thyra ModelEvaluator.
Base class for Runge-Kutta methods, ExplicitRK, DIRK and IMEX.
void testRKAppAction(const Teuchos::RCP< Tempus::StepperRKBase< double >> &stepper, const Teuchos::RCP< const Thyra::ModelEvaluator< double >> &model, Teuchos::FancyOStream &out, bool &success)
Unit test utility for Stepper RK AppAction.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
TEUCHOS_UNIT_TEST(BackwardEuler, Default_Construction)
SolutionHistory is basically a container of SolutionStates. SolutionHistory maintains a collection of...
ACTION_LOCATION
Indicates the location of application action (see algorithm).
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)