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 
16 namespace Tempus_Unit_Test {
17 
18 using Teuchos::RCP;
19 using Teuchos::rcp;
20 using Teuchos::rcp_const_cast;
21 using Teuchos::rcp_dynamic_cast;
23 using Teuchos::sublist;
24 
25 
26 // ************************************************************
27 // ************************************************************
28 TEUCHOS_UNIT_TEST(SDIRK_21Pair, Default_Construction)
29 {
30  auto stepper = rcp(new Tempus::StepperSDIRK_21Pair<double>());
32 
33  // Test stepper properties.
34  TEUCHOS_ASSERT(stepper->getOrder() == 2);
35 }
36 
37 
38 // ************************************************************
39 // ************************************************************
40 TEUCHOS_UNIT_TEST(SDIRK_21Pair, StepperFactory_Construction)
41 {
42  auto model = rcp(new Tempus_Test::SinCosModel<double>());
43  testFactoryConstruction("SDIRK 2(1) Pair", model);
44 }
45 
46 
47 // ************************************************************
48 // ************************************************************
49 TEUCHOS_UNIT_TEST(SDIRK_21Pair, AppAction)
50 {
51  auto stepper = rcp(new Tempus::StepperSDIRK_21Pair<double>());
52  auto model = rcp(new Tempus_Test::SinCosModel<double>());
53  testRKAppAction(stepper, model, out, success);
54 }
55 
56 
57 // ************************************************************
58 // ************************************************************
59 class StepperRKModifierSDIRK21Test
60  : virtual public Tempus::StepperRKModifierBase<double>
61 {
62 public:
63 
65  StepperRKModifierSDIRK21Test(Teuchos::FancyOStream &Out, bool &Success)
66  : out(Out), success(Success)
67  {}
68 
70  virtual ~StepperRKModifierSDIRK21Test(){}
71 
73  virtual void modify(
77  {
78  const double relTol = 1.0e-14;
79  auto stageNumber = stepper->getStageNumber();
80  Teuchos::SerialDenseVector<int,double> c = stepper->getTableau()->c();
81 
82  auto currentState = sh->getCurrentState();
83  auto workingState = sh->getWorkingState();
84  const double dt = workingState->getTimeStep();
85  double time = currentState->getTime();
86  if (stageNumber >= 0) time += c(stageNumber)*dt;
87 
88  auto x = workingState->getX();
89  auto xDot = workingState->getXDot();
90  if (xDot == Teuchos::null) xDot = stepper->getStepperXDot();
91 
92  switch (actLoc) {
94  {
95  {
96  auto DME = Teuchos::rcp_dynamic_cast<
97  const Tempus_Test::DahlquistTestModel<double> > (stepper->getModel());
98  TEST_FLOATING_EQUALITY(DME->getLambda(), -1.0, relTol);
99  }
100  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
101 
102  const double x_0 = get_ele(*(x), 0);
103  const double xDot_0 = get_ele(*(xDot), 0);
104  TEST_FLOATING_EQUALITY(x_0, 1.0, relTol); // Should be x_0
105  TEST_FLOATING_EQUALITY(xDot_0, -1.0, relTol); // Should be xDot_0
106  TEST_ASSERT(std::abs(time) < relTol);
107  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
108  TEST_COMPARE(stageNumber, ==, -1);
109  break;
110  }
113  {
114  const double X_i = get_ele(*(x), 0);
115  const double f_i = get_ele(*(xDot), 0);
116 
117  if (stageNumber == 0) {
118  TEST_FLOATING_EQUALITY(X_i, 1.0, relTol);
119  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
120  TEST_ASSERT(std::abs(f_i) < relTol);
121  } else if (stageNumber == 1) {
122  TEST_FLOATING_EQUALITY(X_i, 0.5, relTol);
123  TEST_FLOATING_EQUALITY(f_i, -1.0, relTol);
124  TEST_ASSERT(std::abs(time) < relTol);
125  } else {
126  TEUCHOS_TEST_FOR_EXCEPT( !(-1 < stageNumber && stageNumber < 2));
127  }
128 
129  break;
130  }
134  {
135  const double X_i = get_ele(*(x), 0); // 0.5
136  const double f_i = get_ele(*(xDot), 0); // -0.5
137 
138  if (stageNumber == 0) {
139  // X_i = 1, f_1 = 0
140  TEST_FLOATING_EQUALITY(X_i, 0.5, relTol);
141  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
142  TEST_FLOATING_EQUALITY(f_i, -0.5, relTol);
143  } else if (stageNumber == 1) {
144  // X_i = , f_i =
145  TEST_FLOATING_EQUALITY(X_i, 0.75, relTol);
146  TEST_FLOATING_EQUALITY(f_i, -0.75, relTol);
147  TEST_ASSERT(std::abs(time) < relTol);
148  } else {
149  TEUCHOS_TEST_FOR_EXCEPT( !(-1 < stageNumber && stageNumber < 2));
150  }
151 
152  break;
153  }
155  {
156  const double x_1 = get_ele(*(x), 0);
157  time = workingState->getTime();
158  TEST_FLOATING_EQUALITY(x_1, 3.0/8.0, relTol); // Should be x_1
159  TEST_FLOATING_EQUALITY(time, 1.0, relTol);
160  TEST_FLOATING_EQUALITY(dt, 1.0, relTol);
161  TEST_COMPARE(stageNumber, ==, -1);
162 
163  if (stepper->getUseEmbedded() == true) {
164  TEST_FLOATING_EQUALITY(workingState->getTolRel(), 1.0, relTol);
165  TEST_ASSERT(std::abs(workingState->getTolAbs()) < relTol);
166  // e = 0 from doxygen above.
167  TEST_ASSERT(std::abs(workingState->getErrorRel()) < relTol);
168  }
169 
170  }
171 
172  }
173 
174 
175  }
176 
177 private:
178 
179  Teuchos::FancyOStream & out;
180  bool & success;
181 };
182 
183 
184 // ************************************************************
185 // ************************************************************
186 TEUCHOS_UNIT_TEST(SDIRK_21Pair, Modifier)
187 {
188  auto stepper = rcp(new Tempus::StepperSDIRK_21Pair<double>());
191 
192  auto modifier = rcp(new StepperRKModifierSDIRK21Test(out, success));
193 
194  stepper->setModel(model);
195  stepper->setAppAction(modifier);
196  stepper->setICConsistency("Consistent");
197  stepper->setUseFSAL(false);
198  stepper->initialize();
199 
200  // Create a SolutionHistory.
201  auto solutionHistory = Tempus::createSolutionHistoryME(model);
202 
203  // Take one time step.
204  stepper->setInitialConditions(solutionHistory);
205  solutionHistory->initWorkingState();
206  double dt = 1.0;
207  solutionHistory->getWorkingState()->setTimeStep(dt);
208  solutionHistory->getWorkingState()->setTime(dt);
209  stepper->takeStep(solutionHistory); // Primary testing occurs in
210  // modifierX during takeStep().
211  // Test stepper properties.
212  TEUCHOS_ASSERT(stepper->getOrder() == 2);
213 }
214 
215 } // namespace Tempus_Unit_Test
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.
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.
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).
void testDIRKAccessorsFullConstruction(const RCP< Tempus::StepperDIRK< double > > &stepper)
Unit test utility for ExplicitRK Stepper construction and accessors.
#define TEUCHOS_ASSERT(assertion_test)
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)